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

fix ParamsFrom type inference #2108

Merged
merged 3 commits into from
Jan 31, 2022
Merged

fix ParamsFrom type inference #2108

merged 3 commits into from
Jan 31, 2022

Conversation

pedroslopez
Copy link
Collaborator

For some reason, TypeScript has trouble inferring types properly when using A extends CLI as a conditional.

image

You can play around with this example here eee6e8d

Separating out ParamsFrom for the CLI to it's own type lets you explicitly use it, though I'd still like to find a solution that keeps things in a single ParamsFrom that works for both if possible, hence the draft PR. Pushed here for visibility.

@@ -22,7 +22,7 @@ export class HelloCliTest extends CLI {
},
},
countries: {
variadic: true as const,
variadic: true as true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to consolidate the Params type, but as const or as true is still required since typescript can't narrow the type well enough itself.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we could also do is make a function called VariadicType that just takes an Input and says what our type is doing. Then we could avoid the as const part.

@evantahler
Copy link
Member

Oh, this is a nice improvement to ParmsFrom either way!

@pedroslopez pedroslopez changed the title separate CLIParamsFrom to its own type fix ParamsFrom type inference Jan 31, 2022
@pedroslopez pedroslopez added the bug Something isn't working label Jan 31, 2022
@pedroslopez pedroslopez marked this pull request as ready for review January 31, 2022 13:53

type Variadic = { variadic: true };

export type ParamsFrom<A extends Action | Task | CLI> = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth putting Action | Task | CLI into a single type, but I don't think this is likely to change.

@evantahler evantahler merged commit 1e374a4 into main Jan 31, 2022
@evantahler evantahler deleted the cli-paramsfrom branch January 31, 2022 16:17
@evantahler
Copy link
Member

evantahler commented Jan 31, 2022

Releasing a new version now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants