Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indicate typescript support in process.features #54294

Closed
silverwind opened this issue Aug 9, 2024 · 5 comments · Fixed by #54295
Closed

Indicate typescript support in process.features #54294

silverwind opened this issue Aug 9, 2024 · 5 comments · Fixed by #54295
Labels
feature request Issues that request new features to be added to Node.js. strip-types Issues or PRs related to strip-types support

Comments

@silverwind
Copy link
Contributor

silverwind commented Aug 9, 2024

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.typescript to indicate whether --experimental-strip-types is enabled.

What alternatives have you considered?

It's possible to check for the presence of --experimental-strip-types in either process.execArgv or process.env.NODE_OPTIONS, but doing so is quite cumbersome.

@silverwind silverwind added the feature request Issues that request new features to be added to Node.js. label Aug 9, 2024
@RedYetiDev RedYetiDev added the strip-types Issues or PRs related to strip-types support label Aug 10, 2024
@anonrig
Copy link
Member

anonrig commented Aug 10, 2024

You can check it by accessing and checking process.argv - https://nodejs.org/docs/latest/api/process.html#processargv

@silverwind
Copy link
Contributor Author

silverwind commented Aug 11, 2024

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 on execArgv. Also, those do not work when NODE_OPTIONS is 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");

@silverwind
Copy link
Contributor Author

As for my use case of loading a config file, I think I don't strictly need this given that Promise.any can 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;
}

@anonrig
Copy link
Member

anonrig commented Aug 11, 2024

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")

@silverwind
Copy link
Contributor Author

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 experimental prefix is removed or when the setting is made the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. strip-types Issues or PRs related to strip-types support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants