Node.js supports stripping TypeScript types with the --experimental-strip-types flag. This flag was introduced in 22.6.0 and will be turned on by default soon(tm) and backported to older versions.
And simply replaces any TypeScript specific syntax with whitespace:
export function myFunction (name ) {
return `hello ${name}`
}
This means Node.js can run TypeScript files without a transpilation/validation step, without depending on tsc or having to know about a project's special unique tsconfig.json setup, and also without needing to add source maps, since all the line numbers/locations are unchanged between the .ts source and the executed JavaScript.
There are a few caveats:
Preprocessing .ts files like this only happens in the current project, not in the node_modules folder so projects will still need to ship transpiled .js files (this will likely apply to monorepo sibling packages too, they'll still have to be built before a dependant sibling can run their code)
It'll only run on .ts files, so all import statements must import .ts
For us only the last two things will cause headaches.
The replacement of .js with .ts will need a PR to aegir to enable the allowImportingTsExtensions tsconfig option and likely an upgrade to the typescript dependency to 5.7.x when it is available, after that it is a relatively simple find & replace operation.
For code that requires transpilation, we only have a few enums in the codebase - I don't think we use any other features that need it. Enabling the --experimental-transform-types flag will cause source maps to be generated which we may wish to avoid - we may be better of refactoring our enums to just be regular JS objects.
Node.js supports stripping TypeScript types with the --experimental-strip-types flag. This flag was introduced in
22.6.0
and will be turned on by default soon(tm) and backported to older versions.It takes a TypeScript file like:
And simply replaces any TypeScript specific syntax with whitespace:
This means Node.js can run TypeScript files without a transpilation/validation step, without depending on
tsc
or having to know about a project's special uniquetsconfig.json
setup, and also without needing to add source maps, since all the line numbers/locations are unchanged between the.ts
source and the executed JavaScript.There are a few caveats:
.ts
files like this only happens in the current project, not in thenode_modules
folder so projects will still need to ship transpiled.js
files (this will likely apply to monorepo sibling packages too, they'll still have to be built before a dependant sibling can run their code).ts
files, so allimport
statements must import.ts
tsc
require an additional --experimental-transform-types flagFor us only the last two things will cause headaches.
The replacement of
.js
with.ts
will need a PR to aegir to enable theallowImportingTsExtensions
tsconfig option and likely an upgrade to thetypescript
dependency to5.7.x
when it is available, after that it is a relatively simple find & replace operation.For code that requires transpilation, we only have a few enums in the codebase - I don't think we use any other features that need it. Enabling the
--experimental-transform-types
flag will cause source maps to be generated which we may wish to avoid - we may be better of refactoring our enums to just be regular JS objects.