-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Feature request: strictPartial mode #21159
Comments
I am not sure i understand the proposal here. but i think the underlying issue is that the type system does not differentiate between a property set to |
Hm, it does seem related to that bug, but this is more about having a way to forbid writing |
#13195 is exactelly what u are talking about. Making optional properties not accept undefined as a valid value. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
This is a request to have a mode that has stricter optional object keys, such as the ones generated by the
Partial
mapped type. The idea is to allow reading a possibly-missing key, where its type will include|undefined
, but not writing.This is important for use cases that involve
Object.assign
or object spreads. Having a key assigned toundefined
will overwrite an existing key by putting the valueundefined
, which currently allows you to break type correctness.Code
Example of current code that breaks because there is no strictPartial:
The type definitions for React currently works around it by using generics,
keyof
andPick
, but this hack can only be used for function arguments, and the function must be converted into a generic function.https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d0fdb378/types/react/index.d.ts#L283-L289
If a type actually means to accept
undefined
itself as a valid value, then its declaration should include|undefined
. An additional standard mapped type (name 🚲🏠) can be used to not only make keys optional but also|undefined
them, and those would be appropriate for use cases such as inputs to object destructuring with defaults.The text was updated successfully, but these errors were encountered: