Sitelet https://github.com/nodejs/node/issues/54294
Skip to content

Indicate typescript support in process.features #54294

Description

@silverwind

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.

Activity

  1. added
    strip-typesIssues and PRs related to TypeScript type stripping.
    on Aug 10, 2024
  2. anonrig commented on Aug 10, 2024

    @anonrig
    Member

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

  3. silverwind commented on Aug 11, 2024

    @silverwind
    ContributorAuthor

    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");
  4. silverwind commented on Aug 11, 2024

    @silverwind
    ContributorAuthor

    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;
    }
  5. anonrig commented on Aug 11, 2024

    @anonrig
    Member

    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")
    
  6. silverwind commented on Aug 12, 2024

    @silverwind
    ContributorAuthor

    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.

  7. moved this from Awaiting Triage to In Progress in Node.js feature requestson Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.strip-typesIssues and PRs related to TypeScript type stripping.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions