This is a work in progress, so you can read more about this on NPM.
Another important limitation is that the type keyword is required for type imports. So whenever you are importing a type, you need to specify that that is a type. For example, in the first case, we import type 1 and 2. We use the type keyword. If we don't use it, Node.js will think that that is a value and not a type, so it will not erase it. While if it's a type, it can be safely erased, and it will cause a runtime error.
Also, an important limitation is that it's not possible to run TypeScript files in Node modules. This is to avoid disrupting the ecosystem because by publishing TypeScript, it would create a lot of incompatibilities. So if you ever try to run a TypeScript file inside the Node modules, we draw an error called unsupported Node modules TypeScript. The reason why this was specifically enforced by the TypeScript team is because it would cause a lot of compatibility versions, and your IDE would perform type checkings on Node modules, and we know how big Node modules are, so we don't want to do this. In Node 23.6, we enabled experimental strip types by default. So you can run Node file.ts by default without passing any flag.
Also, we have worked on TypeScript syntax detection in Eval. This is a very interesting topic. So this is, for example, we can see import util from util, and cost foo is a string value a word. This is a TypeScript ESM string. So Node.js tries to evaluate if the syntax is a common JS module, which it's not. It will try again as an ES module, but it's not because it's TypeScript. So that will fail. If it fails, it will try to strip the types, so it will try to transpile, like remove the string from this, the dot string type from this string. So foo now, this is plain JavaScript, and this is ESM plain JavaScript. So if this fails, it means that you have a syntax error, or the syntax is unsupported. So it will throw the error from the step two, plus the TypeScript compiler message. This is to avoid breaking changes in the error that is thrown. And it will try again to execute the string as a common JS, but we know it's ESM, so it will fail. And it will finally try again as a ESM module, which is correct. So you can see the syntax detection is very complicated. But you can skip all of this overhead by specifying which kind of syntax it is. So you can use the flag dash dash input type to say that this is a module TypeScript, or common JS TypeScript, and Node will know exactly what to do.
Comments