Experimental TypeScript Execution in Node.js 23
Node.js 23’s experimental --experimental-strip-types flag lets developers run TypeScript files directly by stripping type annotations at runtime, eliminating a separate compilation step, but it performs no type checking, so tsc should still be used for safety, making it useful for rapid prototyping while remaining experimental.
TLDR – Key Points
Node.js 23 allows running TypeScript files without prior transpilation.
The --experimental-strip-types flag removes type annotations at runtime.
No type checking is performed – use tsc to ensure safety.
Great for rapid prototyping but does not replace TypeScript tooling.
Feature is experimental and may change in future releases.
Node.js 23 introduces an experimental capability that lets developers execute TypeScript files directly, simplifying the development workflow by removing the need for a separate compilation step.
This article explores how the feature works, the type‑handling mechanism in Node.js 23, and its impact on type safety.
Experimental TypeScript Execution in Node.js 23
Node.js 23 adds an experimental flag --experimental-strip-types which strips type annotations at runtime, allowing direct execution of .ts files without using tsc or Babel.
How to Enable TypeScript Execution
To try this feature you need:
Install Node.js 23 or newer.
Prepare a TypeScript file (e.g., .ts).
Run the script with the --experimental-strip-types flag.
How TypeScript Types Are Handled in Node.js 23
The feature does not perform runtime type checking. It simply removes type annotations before execution, so the code runs as plain JavaScript.
Example 1: Running a Simple TypeScript Function
Let's demonstrate with a basic function.
Step 1: Create a TypeScript file ( example.ts )
function addNumbers(a: number, b: number): number {
return a + b;
}
console.log(addNumbers(5, 10));Step 2: Execute the TypeScript file directly
node --experimental-strip-types example.tsExpected Output
15Node.js removes the type annotation : number and runs the JavaScript equivalent.
Example 2: Type‑Safety Considerations
Because Node.js 23 does not perform static type checking, passing an incorrect type will not raise an error.
Type Mismatch Example
function greet(name: string) {
return `Hello, ${name}!`;
}
// This is a type error in TypeScript, but Node.js will execute it.
console.log(greet(42));Output
Hello, 42!Normally TypeScript would report:
Argument of type 'number' is not assignable to parameter of type 'string', but the experimental flag strips the type and runs the code.
Solution: Use tsc for Type Checking
To ensure type safety, run the TypeScript compiler before execution: tsc example.ts This checks for type errors prior to running the script.
Key Takeaways
✔ Node.js 23 allows direct execution of TypeScript files without pre‑compilation.
✔ It removes type annotations at runtime but does not perform type checking .
✔ Use tsc to catch type errors before running.
✔ The feature is experimental and may change in future Node.js versions.
Conclusion
The experimental TypeScript execution feature in Node.js 23 provides a more flexible and efficient development experience. While it simplifies running TypeScript files, developers should still rely on static type‑checking tools to maintain code safety. Combining Node.js with proper TypeScript tooling enables building robust and maintainable applications.
Meta Description: Node.js 23 introduces the experimental --experimental-strip-types flag for TypeScript execution. Learn how it works, its limitations, and best practices for ensuring type safety.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
