-
Notifications
You must be signed in to change notification settings - Fork 12.8k
"not in keyof T" - exclude keyof T from string #42315
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
Comments
I see - thanks for the quick response ;) |
To expand a bit on this. The |
Awesome thanks for the additional elaborations. Sounds like the right solution would be based on negated types #29317 when it's available - something like: type NotKeyof<T> = string & not keyof T |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@yinso You mention being able to do your incremental object creation just without the safety of adding a property multiple times. Do you mind sharing how you were able to achieve that? I'm in a similar boat right now. |
@justindbaur this TS Playground fiddle shows a simple sketch of the incremental idea. let me know if there are any questions. |
Suggestion
๐ Search Terms
โ Viability Checklist
My suggestion meets these guidelines:
โญ Suggestion
I am trying to define a type that matches all strings except for
keyof T
, which seems to be definable as:However, this doesn't work the way I thought it would. TypeScript currently seems to treat
string
and the keys of an object as distinct types, so the exclusion doesn't do anything.This could just be my search skill failing me - if this is already doable in some form, please let me know thanks.
๐ Motivating Example
This reason I am looking for such a type is to write code that allows for incrementally building up an object type, so as I add new keys, the type checker can help catch whether I accidentally used old keys.
I am seeing the need for this pattern in the following scenarios where there is a need to deal with version management and incremental changes - I am sure there are more elsewhere as well:
๐ป Use Cases
My current usage is to model database schema changes. The following snippet shows what I am thinking of:
By allowing the type to be incrementally build up, we can model multiple versions of the database in a single code base.
This is already doable today with most of TypeScript's facilities. The gap right now is that when I add a new column, it would be great for TS to help check whether the key was already used, hence the "not in keyof" test:
If this is not yet doable in TS, it seems to me this should be a safe addition to existing code, but certainly would love to know more either way on how this can be considered. Please let me know if there are any questions, thanks.
The text was updated successfully, but these errors were encountered: