-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Partial type fails to narrow in spite of check for undefined #29496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Update: this particular version of the bug is fixed in 3.3RC. But the original bug ( A similar error in a slightly different context: type GeoType2 = GeoPoint2 | GeoRoute2;
type GeoPoint2 = {
type: 'Point';
coordinates: {};
id: number;
};
type GeoRoute2 = {
type: 'LineString';
coordinates: {}[];
start: { id: number, name: string };
end: { id: number, name: string };
};
function foo(value: Partial<GeoType2>) {
switch (value.type) {
case 'Point':
return;
case 'LineString':
return !!value.start && !!value.end && !!value.start.id && !!value.end.id && !!value.coordinates
&& value.start.name && value.end.name; // HERE
default: return never(value.type);
}
} The compiler reports an error for undefined access at the point marked "HERE". The |
Your first case is correct behaviour according to the definition of
EDIT: Proof of this is const value: Partial<UnionOfBar> = { type: undefined } This is valid and type checks correctly |
@dhruvrajvanshi there is a check for |
Welp. I missed the undefined check. It does seem like a genuine bug to me. Sorry about that. Converting the switch case to equivalent if else statements does seem to work so I don't see why this wouldn't work. |
The playground is actually working correctly for both reported bugs. But following the download link from the playground for Visual Studio 2017 and installing version 3.2.2.0 causes the same error. |
I think this is fixed in TypeScript 3.2.4 but there is no download link to this version. https://www.microsoft.com/en-us/download/details.aspx?id=55258 @DanielRosenwasser is there a download link for TS 3.2.4 so that I can test and close this issue? Thanks! |
The bug is still present in TS 3.3RC. |
I'm afraid I can't offer up a solution, but in terms of tracing the problem this looks to be something affected by this fix here #27612. Existing narrowings on properties, such as This is the comment by @ahejlsberg regarding the issue:
|
The code below works in TS 3.0.
TypeScript Version: 3.2.1.0
Search Terms: is:issue 3.2 Partial
Code
Expected behavior:
No compiler error
Actual behavior:
Type 'undefined' is not assignable to parameter of type 'never'
Playground Link:
http://www.typescriptlang.org/play/#src=declare%20function%20never(value%3A%20never)%3A%20never%3B%0D%0A%0D%0Aconst%20enum%20BarEnum%20%7B%0D%0A%20%20%20%20%20%20%20%20bar1%20%3D%201%2C%0D%0A%20%20%20%20%20%20%20%20bar2%20%3D%202%2C%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20type%20UnionOfBar%20%3D%20TypeBar1%20%7C%20TypeBar2%3B%0D%0A%0D%0A%20%20%20%20type%20TypeBar1%20%3D%20%7B%0D%0A%0D%0A%20%20%20%20%20%20%20%20type%3A%20BarEnum.bar1%3B%0D%0A%20%20%20%20%7D%3B%0D%0A%0D%0A%20%20%20%20type%20TypeBar2%20%3D%20%7B%0D%0A%0D%0A%20%20%20%20%20%20%20%20type%3A%20BarEnum.bar2%3B%0D%0A%20%20%20%20%7D%3B%0D%0A%0D%0A%20%20%20%20const%20value%3A%20Partial%3CUnionOfBar%3E%20%3D%20%7B%7D%20as%20any%3B%0D%0A%0D%0A%20%20%20%20if%20(value.type%20!%3D%3D%20undefined)%20%7B%0D%0A%0D%0A%20%20%20%20%20%20%20%20switch%20(value.type)%20%7B%0D%0A%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20BarEnum.bar1%3A%20break%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20BarEnum.bar2%3A%20break%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20default%3A%20never(value.type)%3B%20%2F%2F%20Error%3A%20Type%20'undefined'%20is%20not%20assignable%20to%20parameter%20of%20type%20'never'%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D
Related Issues:
#28748
The text was updated successfully, but these errors were encountered: