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: improve flag types #456

Merged
merged 1 commit into from Aug 6, 2022
Merged

fix: improve flag types #456

merged 1 commit into from Aug 6, 2022

Conversation

mdonnalley
Copy link
Contributor

@mdonnalley mdonnalley commented Aug 5, 2022

Because of how globalFlags is typed, if you do not define any global flags then the flags returned from Command.parse will include { [flag: string]: unknown }, which prevents typescript from erroring when you're attempting to access a non-existent flag.

This PR changes the default type for flags and globalFlags from { [flag: string]: unknown } to object. Doing so removes the index signature and allows typescript to fail when accessing a non-existent flag. Unfortunately, this requires using the object type which is discouraged.

The alternative to this wrapping the returned flags in a new generic type that removes the index signature,

type Knowable<T> = {
   [ K in keyof T as string extends K ? never : number extends K ? never : K ] : T[K]
};

This also solves the problem but it slightly obscures what the actual flags are, e.g.

Screen Shot 2022-08-05 at 1 17 48 PM

@mdonnalley mdonnalley self-assigned this Aug 5, 2022
src/interfaces/parser.ts Show resolved Hide resolved
Copy link
Member

@mshanemc mshanemc left a comment

Choose a reason for hiding this comment

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

yay, object

@mshanemc mshanemc merged commit 704d42c into main Aug 6, 2022
@mshanemc mshanemc deleted the mdonnalley/improve-flag-types branch August 6, 2022 00:00
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.

None yet

2 participants