-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Control flow doesn't recognise undefined check #12600
Comments
Actually not only interface Properties {
"property1"?: string
}
function getMap(): {[index: string]: Properties | null} {
return {
"1": {
"property1": "test"
},
"2": null
};
}
let map = getMap();
let item1Property1 = "1" in map && map["1"] !== null && "property1" in map["1"] ? map["1"].property1 : null; The above code cannot be compiled when
|
We currently consider a type a discriminated union only when all of the possible discriminant values have types composed from unit types. Effectively this means that the |
Related: #12457 |
Tentative work item to investigate for the 2.4 milestone, approximately. |
Here's another example of a failing case: declare var maybeNull: number[] | undefined | null;
let isEmpty: boolean | undefined | null = maybeNull === undefined || maybeNull === null
? false
: maybeNull; // Type of maybeNull should be `null | undefined`
// Instead error is:
// Type 'false | number[]' is not assignable to type 'boolean'.
// Type 'number[]' is not assignable to type 'boolean'. |
@eggers You just have your cases backwards: declare var maybeNull: number[] | undefined | null;
let isEmpty: boolean | undefined | null = maybeNull === undefined || maybeNull === null
? maybeNull // Type of maybeNull _is_ `null | undefined`
: false; |
Closing as the original example is no longer an error. |
TypeScript Version: 2.1.1
Code
Expected behavior:
No error with strict null checks.
Actual behavior:
Thinks
value.stdout
could beundefined
on last line, but this is not possible.The text was updated successfully, but these errors were encountered: