Repository navigation
Indicate typescript support in process.features #54294
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Aug 9, 2024 - addedstrip-typesIssues and PRs related to TypeScript type stripping.Issues and PRs related to TypeScript type stripping.
on Aug 10, 2024 You can check it by accessing and checking process.argv - https://nodejs.org/docs/latest/api/process.html#processargv
You can check it by accessing and checking process.argv - https://nodejs.org/docs/latest/api/process.html#processargv
Node-specifc options are not present in
argv, only onexecArgv. Also, those do not work whenNODE_OPTIONSis in use:$ NODE_OPTIONS="--experimental-strip-types" node -p process.argv [ '/opt/homebrew/Cellar/node/22.6.0/bin/node' ]Writing such a check currently would be clumsy and break if the experimental prefix is removed or the option is enabled by default:
const supportsTypescript = (process.env.NODE_OPTIONS ?? "").split(/\s+/).includes("--experimental-strip-types") || process.execArgv.includes("--experimental-strip-types");
As for my use case of loading a config file, I think I don't strictly need this given that
Promise.anycan solve this pretty nicely as one could just ignore the import error like this:async function loadConfig(rootDir: string): Promise<Record<string, any>> { let config: Record<string, any> = {}; try { ({default: config} = await Promise.any(["mod.config.ts", "mod.config.js"].map(file => { return import(path.join(rootDir, file)); }))); } catch {} return config; }
I think this can still be easily supported on userland with a single line:
const hasTypescript = process.argv.includes("--experimental-strip-types") ?? process.env.NODE_OPTIONS?.includes("--experimental-strip-types")I think this can still be easily supported on userland with a single line:
const hasTypescript = process.argv.includes("--experimental-strip-types") ?? process.env.NODE_OPTIONS?.includes("--experimental-strip-types")This works now, but it's bound to break once the
experimentalprefix is removed or when the setting is made the default.
What is the problem this feature will solve?
Command line applications that want to load typescript configuration files should be able to detect whether the current node process is able to import typescript files to avoid an error during async
import.What is the feature you are proposing to solve the problem?
Add boolean value
process.features.typescriptto indicate whether--experimental-strip-typesis enabled.What alternatives have you considered?
It's possible to check for the presence of
--experimental-strip-typesin eitherprocess.execArgvorprocess.env.NODE_OPTIONS, but doing so is quite cumbersome.