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
typeWeekDay="Monday"|"Tuesday"|"Wednesday"|"Thursday"|"Friday"|"Saturday"|"Sunday";functiondoFun(a: WeekDay|WeekDay[]){a="Monday";// string literal is contextually typed, so it has string literal type "Monday" => no type mismatchif(a==="Monday"){// string literal is NOT contextually typed and has type 'string' => should be type mismatch error}}
Type mismatch error in the second case should be because:
we're comparing 'string' with "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday" | ("Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday")[] for assignability in both directions
according to the spec, in direction 'string' => the-other-type, we're comparing 'string' with all union type constituents, and it's not assignable to any of them => fail
according to the spec, in direction the-other-type => 'string', all constituents of the-other-type should be assignable to 'string', but obviously Array is not assignable to it => fail
both conditions fail => type mismatch error for '===' operator
But by some reason the compiler doesn't show any errors here.
Is there some special handling of string literal types with operators like '===', switch labels, etc?
The text was updated successfully, but these errors were encountered:
We have two alternative proposals to solving this better. One is inferring string literal types at comparison locations (see the PR at #6196), and the other is described on #6167 (comment).
Type mismatch error in the second case should be because:
But by some reason the compiler doesn't show any errors here.
Is there some special handling of string literal types with operators like '===', switch labels, etc?
The text was updated successfully, but these errors were encountered: