When JavaScript Meets Hardware: A New Chapter in Embedded
When we say "embedded development," associations with C, bare metal, assembly inserts, and endless debugging sessions with an oscilloscope usually come to mind. Now imagine writing firmware for a smart temperature sensor using TypeScript. Sounds like science fiction? It shouldn't.
The world of embedded systems is experiencing a quiet revolution. Modern microcontrollers now have enough memory and clock speed to run scripting languages. And TypeScript, with its static typing and rich ecosystem, is becoming an ideal candidate for embedded projects where code reliability and readability are crucial.
In this article, we'll explore how TypeScript is entering the embedded world, what tools are needed, and write real code to control an LED via GPIO. Let's go!
Why TypeScript Instead of C or Python?
The classic choice for embedded is C. It's fast, predictable, and uses little memory. But it has a dark side: manual memory management, lack of proper compile-time type checking, and macro "hell." Python (MicroPython) is simpler but slow and dynamic — you'll only discover a bug at runtime when the device freezes on a shelf somewhere.
TypeScript offers a sweet spot:
- Strong typing — the compiler catches errors at build time. No accidental
stringinstead ofnumberin microcontroller registers. - Modern syntax — async/await, generics, interfaces. Code becomes self-documenting.
- npm ecosystem — thousands of libraries that can be adapted for hardware.
- Type safety — you know exactly what you're passing to the
setPin(17, HIGH)function, notsetPin("17", true).
Of course, TypeScript won't replace C for high-load real-time systems. But for IoT sensors, smart bulbs, weather stations, and prototypes — it's an ideal choice.
Toolkit: What You Need to Get Started
To write TypeScript for hardware, we need a runtime that can interpret JavaScript on a microcontroller. The most popular options are:
- JerryScript — a lightweight JS engine from Samsung. Takes up ~1 MB of Flash.
- Espruino — a specialized firmware for STM32 and ESP32. Supports TypeScript via transpilation.
- Moddable SDK — a powerful IoT framework with built-in TypeScript support.
We'll use Espruino on an ESP32 board (or Raspberry Pi Pico with Espruino firmware). This is the easiest way to try TypeScript in embedded without a soldering iron or datasheets.
Installation and Setup
- Download the Espruino firmware for your board from the official website.
- Flash the board via USB (use the
esptoolutility for ESP32). - Install Node.js and npm if not already installed.
- Install
espruino-cliglobally:npm install -g espruino-cli. - Create a project:
mkdir my-embedded-ts && cd my-embedded-ts && npm init -y. - Install TypeScript:
npm install --save-dev typescript @types/espruino.
Now you have a TypeScript project that knows about Espruino types! Create a tsconfig.json file:
{ "compilerOptions": { "target": "ES2015", "module": "commonjs", "strict": true, "outDir": "./dist", "rootDir": "./src", "types": ["espruino"] }, "include": ["src/**/*"]}