Skip to content

feat: ship TypeScript type definitions - #307

Merged
gengjiawen merged 4 commits into
mainfrom
feat/typescript-types
Oct 7, 2026
Merged

gengjiawen merged 4 commits into
mainfrom
feat/typescript-types

Conversation

@gengjiawen

Copy link
Copy Markdown
Collaborator

Ships type definitions with the package (index.d.ts), plus tests that keep them in sync with src/. The existing @types/envinfo has drifted: it is missing 23 of the 106 helpers (gh, glab, ripgrep, Cursor, Claude Code, bun, Deno, Zig, …) and the Monorepos / pnpmGlobalPackages categories, and it types helper results as Promise<string> when they resolve to [name, version, path].

Category items are typed by name, so typos fail to compile:

run({ Utilities: ['gh', 'glab', 'ripgrep'] }); // ok
run({ Utilities: ['Ripgrep'] });                // error
run({ preset: 'vite' });                        // error: unknown preset

How the types are tested

yarn test:types runs both checks under TypeScript 7 and TypeScript 6:

  • Contract with src/ (test-d/contract.test-d.ts): with allowJs, tsc infers the exports of src/helpers and src/presets, and the test compares them with index.d.ts in both directions: helper names, categories, the items in each category, preset names, and whether every item has a helper and every preset is a valid Config. presets.js gets a JSDoc /** @type {const} */ cast so tsc keeps the literal names. The cast does nothing at runtime. The errors name what drifted:
    error TS2344: Type '"CategoryItems['Utilities'] is missing gh"' does not satisfy the constraint 'never'.
    error TS2344: Type '"HelperNames is missing getghInfo"' does not satisfy the constraint 'never'.
    
    To confirm the test is not vacuous, I broke six things on purpose (a missing item, a missing helper, a new preset item, the NVim bug below, a preset typo, a stale preset name); each one was caught.
  • Usage (test-d/index.test-d.ts): the README example, valid calls, and @ts-expect-error cases under strict + exactOptionalPropertyTypes.
  • Packaging (yarn check:package): runs attw on the packed tarball. It passes for node10, node16 (CJS and ESM) and bundler resolution.

A new types CI job runs all three.

Fixes found along the way

  • Neovim was never detected. presets.defaults listed 'NVim', but the helper is getNvimInfo and it reports 'Nvim'. The contract test caught this. The unsupported-platform branch also reported 'Vim' instead of 'Nvim', so I fixed that too.
  • Named ESM imports crashed. import { run } from 'envinfo' threw SyntaxError: Named export 'run' not found, because webpack's commonjs2 bundle hides its exports from Node's cjs-module-lexer. @types/envinfo already allowed this import, so this is not new. A small webpack plugin now appends esbuild's no-op 0 && (module.exports = { cli, helpers, main, run }) hint after minification. Default import, require and the CLI are unchanged.

Other changes

  • CI tests on Node.js 24 and 26 (was 22 and 24). @types/node is aligned to 24.
  • README: short TypeScript section, plus a note in Contributing about index.d.ts.

Not fixed here (found while checking helper names; happy to send follow-ups)

  • run({ preset: 'cssnano' }) throws synchronously ((value || []).map is not a function). run() passes the preset's options key through as a category, while cli() strips it. This affects cssnano, create-react-app and react-native-web.
  • getVMwareFusionInfo reports 'VMWare Fusion', which doesn't match the 'VMware Fusion' item, so it never shows up.
  • getEdgeInfo resolves to the string 'N/A' (not a tuple) on Linux.
  • getPodmanInfo exists but isn't listed in any category.

Testing

  • yarn test (jest, oxlint, prettier, type tests under TS 7 and TS 6) passes on Node 24 and Node 26.
  • yarn build, yarn check:package and node src/cli.js --all pass on Node 24 and Node 26.
  • Under Node 26 ESM, both import envinfo from 'envinfo' and import { run } from 'envinfo' work against dist/.

After release, @types/envinfo on DefinitelyTyped can be deprecated in favor of the bundled types.

BEGIN_COMMIT_OVERRIDE
feat: ship TypeScript type definitions
fix: detect Neovim in the IDEs category
fix: support named imports of envinfo from ESM
END_COMMIT_OVERRIDE

presets.defaults listed 'NVim', but the helper is getNvimInfo and reports
'Nvim', so Neovim was never looked up. Also report 'Nvim' (not 'Vim') on
unsupported platforms so it cannot overwrite the Vim entry.
The commonjs2 bundle assigns module.exports at runtime, so Node's
cjs-module-lexer finds no named exports and `import { run } from 'envinfo'`
throws in ESM. Append the no-op `0 && (module.exports = { ... })` hint
after minification so Node can detect cli, helpers, main and run.
Add index.d.ts with category items, presets, options and helpers typed
by name, and test it with TypeScript 6 and 7:

- test-d/contract.test-d.ts checks the types against src/ (allowJs), so
  a helper, category item or preset added without types fails to build
- test-d/index.test-d.ts covers valid and invalid usage
- check:package runs attw on the packed tarball

These replace @types/envinfo, which is missing 23 helpers and types
helper results as strings.
Also align @types/node with the oldest tested Node.js version.
@gengjiawen
gengjiawen merged commit c556683 into main Oct 7, 2026
8 of 9 checks passed
@gengjiawen
gengjiawen deleted the feat/typescript-types branch October 7, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant