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
I think the TypeScript team mentioned somewhere that | undefined should be considered equivalent to ? -- i.e. it aligns with their goals. For most intents and purposes this is how TS currently behaves, although I see what you mean by compiler differentiating somehow.
For example Flow treats these (? and | void) as different things, i.e. you could technically have a property and expect its type to be undefined. However for (all?) practical reasons they are pretty much the same.
The text was updated successfully, but these errors were encountered:
I used to suggest that use never for optional parameter, but it seems never is not the right type, see #14452 (comment).
Thus I realize that ? is currently the only way to set a parameter to be optional, for example:
typeA={a: undefined;b: string;}typeX=A['a'];//=> undefinedconsta: A={b: 'str'};// ^ [ts]// Type '{ b: string; }' is not assignable to type '{ a: undefined; b: string; }'.// Property 'a' is missing in type '{ b: string; }'.typeB={a: never;b: string;}typeY=B['a'];//=> neverconstb: B={b: 'str'};// ^ [ts]// Type '{ b: string; }' is not assignable to type 'B'.// Property 'a' is missing in type '{ b: string; }'.typeC={a?: undefined;b: string;}typeZ=C['a'];//=> undefinedconstc: C={b: 'str'};// passed
Based on a comment:
TypeScript Version: 2.5.0-dev.20170626
Code
Expected behavior:
Probably no error, since requiring
never
/undefined
on a prop seems sorta useless (it'd default to that anyway).Actual behavior:
Error:
Property 'a' is missing in type '{ c: 1; }'
How much of this is by design?
Related comment by @niieani:
The text was updated successfully, but these errors were encountered: