You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 to undefined will overwrite an existing key by putting the value undefined, which currently allows you to break type correctness.
Code
interfaceFoo{bar: string}constfoo1: Foo={bar: ''}// okconstfoo2: Partial<Foo>={}// okconstfoo3: Partial<Foo>={bar: ''}// okconstfoo4: Partial<Foo>={bar: undefined}// type error under --strictPartialfunctiontest(foo: Foo,partial: Partial<Foo>){constbar1=foo.bar// typeof bar1 == stringconstbar2=partial.bar// typeof bar2 == string|undefined}test(foo1,foo1)test(foo1,foo2)test(foo1,foo3)// if some fancy type tracking is done on foo3, otherwise type errortest(foo3,foo3)
Example of current code that breaks because there is no strictPartial:
interfaceFoo{bar: string}constfoo: Foo={bar: ''}constpartial: Partial<Foo>={bar: undefined}// would be an error under --strictPartialconstmerged: Foo={ ...foo, ...partial}// acceptedconst{ bar }=merged// typeof bar is string// bar is actually `undefined`!
The type definitions for React currently works around it by using generics, keyof and Pick, but this hack can only be used for function arguments, and the function must be converted into a generic function.
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:
Jessidhia
changed the title
Feature request: strictPartial types
Feature request: strictPartial mode
Jan 12, 2018
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 undefined and one that is missing. this is tracked by #13195
Hm, it does seem related to that bug, but this is more about having a way to forbid writingundefined into an optional property. When the property is read, it should be accepted as possibly being undefined because it is marked as optional with ?:.
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: