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
Can't distinguish whether a property is present vs. when it's undefined.
Comes up in functions like React's setState.
You don't want certain properties to be explicitly undefined. setState just allows you to omit properties of existing state.
Specifically:
this.setState({foo: "hello",bar: undefined});
has a different meaning in React than
this.setState({foo: "hello"});
Previously were considering missing as an alternative to undefined to describe the distinction. It's kind of messy and confusing for people to have another type.
Idea: have undefined affect reading values (i.e. read side of a type), write type only describes the explicitly given type.
Means obj.a = obj.a is not always allowed! But kind of correct.
This behavior now also allows you to narrow by in and then perform the obj.a = obj.a assignment.
Breaking change - but if we did it all over again, we'd do it this way.
So strict flag?
Automated addition of | undefined to a property avoids breaking changes.
Maybe the case that optionality not including an explicit undefined is actually what users meant...but maybe not really? Many (most?) programs probably just check the value of a property.
Similar to whether you want to have an "optional" type that doesn't distinguish between null and undefined. Most programs don't care, but it was useful for us to distinguish between that!
Ship a codemod?
Do we support delete in control flow analysis?
Don't think so.
This might kind of force people to do that?
Could consider doing it.
Could also have ? to mark optionality on an index signature which piece-meal allows you to opt into noUncheckedIndexedAccess.
Very hard to understand how this works on higher-order with mapped types (e.g. Partial, Pick, Required).
A way to opt into new semantics?
??
{ x??: string }
Mapped types?
typePartial<T>={[KinkeyofT]?: T[K]}
Does this mean partial includes undefined in every property? Does this break existing code that assumes otherwise?
Could have a default type argument that specifies whether you wanted to include undefined or not.
TC39 Update
useDefineForClassFields?Optional vs
undefined#13195
Can't distinguish whether a property is present vs. when it's undefined.
Comes up in functions like React's
setState.You don't want certain properties to be explicitly
undefined.setStatejust allows you to omit properties of existing state.Specifically:
has a different meaning in React than
Previously were considering
missingas an alternative toundefinedto describe the distinction. It's kind of messy and confusing for people to have another type.Idea: have
undefinedaffect reading values (i.e. read side of a type), write type only describes the explicitly given type.obj.a = obj.ais not always allowed! But kind of correct.inand then perform theobj.a = obj.aassignment.Breaking change - but if we did it all over again, we'd do it this way.
strictflag?Automated addition of
| undefinedto a property avoids breaking changes.Maybe the case that optionality not including an explicit
undefinedis actually what users meant...but maybe not really? Many (most?) programs probably just check the value of a property.nullandundefined. Most programs don't care, but it was useful for us to distinguish between that!Ship a codemod?
Do we support
deletein control flow analysis?Could also have
?to mark optionality on an index signature which piece-meal allows you to opt intonoUncheckedIndexedAccess.Very hard to understand how this works on higher-order with mapped types (e.g.
Partial,Pick,Required).A way to opt into new semantics?
??{ x??: string }Mapped types?
undefinedor not.TSConfig Categorization
#13195
--