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

Make generic choices type const #61

Open
wants to merge 4 commits into
base: release/13.x
Choose a base branch
from

Conversation

matthyk
Copy link
Contributor

@matthyk matthyk commented Feb 7, 2024

While working on #59 I noticed that since TypeScript 5.0 it is possible to declare generic types as const. This infers the value as const. The advantage of this is that the command type is more strictly typed. For this to work we need PR #59, but I wanted to make these new changes in a separate PR.

program
  .addOption(new Option('-o, --opt', '').choices(['a', 'b', 'c']))
  .action(function () {
    expectType<Command<[], { opt?: true | 'a' | 'b' | 'c' }>>(this)
  })

instead of

program
  .addOption(new Option('-o, --opt', '').choices(['a', 'b', 'c']))
  .action(function () {
    expectType<Command<[], { opt?: true | string }>>(this)
  })

@shadowspawn
Copy link
Contributor

Oh nice. This is independent of #59 and the work-around is actually covered in the README:

Use a "const assertion" on the choices to narrow the option type from string

@shadowspawn
Copy link
Contributor

shadowspawn commented Feb 7, 2024

Typescript 5 is somewhat "recent" and I think might have to wait for a breaking major update of this package in the future to adopt this, or do more work and support multiple versions: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions

(Assuming we don't already require Typescript 5! Hmm, should have a test that runs against oldest supported TypeScript.)

@shadowspawn
Copy link
Contributor

DefinitelyTyped has a handy graphic of their 2-year TypeScript testing support: https://github.com/DefinitelyTyped/DefinitelyTyped#support-window

@matthyk
Copy link
Contributor Author

matthyk commented Feb 10, 2024

Since one can achieve the same with as const, we should stick to the timeline from DefinitelyTyped

@shadowspawn
Copy link
Contributor

I am keeping an eye out for Typescript dependencies in other projects.

A new version of typescript-eslint is supporting TypeScript >=4.7.4:

@shadowspawn shadowspawn changed the base branch from main to release/13.x September 2, 2024 09:03
@mrazauskas
Copy link

should have a test that runs against oldest supported TypeScript

@shadowspawn

To draw your attention, TSTyche is a type testing tool that is able to use specified version of TypeScript: tstyche --target 4.8,latest. This simple!

It also has test() helper with .only and .skip modifiers. Also --watch mode. And much more.


Note that the install size of tstyche is only around 220kB. This is because the installed TypeScript is loaded by default.

In contrary tsds install size is 48.0MB. They ship their own copy to TypeScript. (Or maybe even two? Because the install size of the latest version of typescript package is 20.9MB.)

@shadowspawn
Copy link
Contributor

Thanks @mrazauskas , I had not seen TSTyche.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants