Closed
Description
type WeekDay = "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday";
function doFun(a: WeekDay | WeekDay[]) {
a = "Monday"; // string literal is contextually typed, so it has string literal type "Monday" => no type mismatch
if (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?