-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
TypeChecker::getTypeAtLocation(Identifier) does not Return Narrowed Type Inside an Array.isArray()-Based Type Guard #8094
Comments
how about |
Doing |
This appears fixed in typescript@next (1.9.0-dev.20160426) -- probably by #8010 ... although, in this version of the code if you add an export function X() {
let x: number | number[] = 0;
if (!Array.isArray(x)) {
const y = x;
} else {
const z = x;
} You end up with the type of variable |
i believe this should be addressed by #8324. |
OK, tried typescript@next (1.9.0-dev.20160428) and now I get |
this is control flow analysis in action :D let x: number | number[] = 0;
// x here can only be a number, you assigned 0 to it
if (!Array.isArray(x)) {
// it is a number not an array alright
const y = x;
} else {
// it was a number, now we can only be in this branch if it is an array, but it ain't. so it is nothing.
const z = x;
} this should do what you expect: let x: number | number[]; // notice, no initialization
if (!Array.isArray(x)) {
const y = x; // number
} else {
const z = x; // number[]
} |
looks like this was fixed. closing. |
@mhegazy That... is... holy!!! I think I'm in love with this 👍 |
1.8.10 / next (1.9.0-dev.20160414)
Code
Behavior:
Why does
TypeChecker::getTypeAtLocation(Identifier::x)
correctly reportnumber
after the first (typeof x === "number"
) & second (typeof x !== "object"
) type guards, but report the (un-narrowed)Union
type after the third (!Array.isArray(x)
) type guard? All three block'sconst c
is correctly typed asnumber
. Is there something besidesTypeChecker::getTypeAtLocation(Identifier)
to use here?The text was updated successfully, but these errors were encountered: