-
Notifications
You must be signed in to change notification settings - Fork 554
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
Introduce StrictKeyOmit
#2968
Introduce StrictKeyOmit
#2968
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a better place for this utility type. I don't know where you keep things like this so I just shoved it here for now
[K in keyof T as IsLiteralKey<K> extends 1 ? K : never]: K | ||
} | ||
|
||
export type StrictKeyOmit<T, K extends LiteralKey<T>> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K extends LiteralKey<T>
is only needed for completions - that's something that you don't quite need because this is only used by a generated code. But the same filtering would have to be done elsewhere without this constraint so it feels nice to have it here
type IsLiteralKey<K extends PropertyKey> = { | ||
string: string extends K ? 0 : 1 | ||
number: number extends K ? 0 : 1 | ||
symbol: symbol extends K ? 0 : 1 | ||
}[K extends string ? 'string' : K extends number ? 'number' : 'symbol'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't 100% complete. It might not handle index signatures with template literal types or string mappings (like Uppercase<string>
) but I seriously doubt you care about any of those here.
In a sense, support for number
and symbol
is already beyond what you need here but it was easy enough to add it so I did it for the completeness' sake
Thanks. I included something like this in #2956 |
fixes #2952
I couldn't run
codegen
script for some reason so I didn't regenerate the output yet. I end up with:This improves type safety by omitting only the desired literal keys when index signatures are present (leaving the index signatures in place!). You can see this in action in this TS playground