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
{{ message }}
Repository navigation
Is there a special 'hack' for operators and string literal types? #6505
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?
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).
Activity
zhuravlikjb commentedon Jan 15, 2016
Ah, I've found it, sorry:
8dbfe1c
DanielRosenwasser commentedon Jan 15, 2016
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).
zhuravlikjb commentedon Jan 15, 2016
Thanks a lot, Daniel! Will track both PRs to see the final decision taken.
DanielRosenwasser commentedon Feb 1, 2016
Anton Lobov (@zhuravlikjb) things are leaning towards literal locations.