-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Annotation of boolean type ignore in favor of narrowed type #31734
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
Union types are narrowed on assignment. |
Ah yeah I could reproduce the error with a different union type
Seems like an explicit annotation should override narrowing though? Explicit seems like it should always win over any type of inference. |
Intuitively, there should really not be a difference between let u: U = "a";
/* some statement */ and let u: U;
u = "a";
/* some statement */ |
Huh, intuitively I think of the things on the left of an assignment as happening after the things on the right. Maybe this is my C++ background coming through (https://godbolt.org/z/VvVzbE). I guess for javascript both ways are equally valid though. |
Cross-linking to #8513, of which this issue is arguably a duplicate |
This behavior is unintuitive to me, as well. In particular, with aliased object types, adding a widening type annotation can actually result in narrowing: type Open = { isOpen: true };
type Closed = { isOpen: false };
type Status = Open | Closed;
const openBare = { isOpen: true };
const openAnnotated: Status = { isOpen: true };
type Bare = typeof openBare; // { isOpen: boolean }
type Annotated = typeof openAnnotated; // { isOpen: true } |
TypeScript Version: 3.5.1
Search Terms:
Boolean explicit annotation ignore
Code
Expected behavior:
4th example above shouldn't error.
Actual behavior:
Error: This condition will always return 'false' since the types 'true' and 'false' have no overlap.
Playground Link:
https://www.typescriptlang.org/play/#src=%2F%2F%20Errors%20as%20expected%0D%0Alet%20bothInferred%20%3D%20true%3B%0D%0Aif%20(bothInferred%20%3D%3D%20false)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20Doesn't%20error%20as%20expected%20%0D%0Alet%20bothExplicit%3A%20boolean%20%3D%20true%20as%20boolean%3B%0D%0Aif%20(bothExplicit%20%3D%3D%20false)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20Doesn't%20error%20as%20expected%0D%0Alet%20leftInferredRightExplicit%20%3D%20true%20as%20boolean%3B%0D%0Aif%20(leftInferredRightExplicit%20%3D%3D%20false)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20Why%20error%3F%0D%0Alet%20rigthInferredLeftExplicit%3A%20boolean%20%3D%20true%3B%0D%0Aif%20(rigthInferredLeftExplicit%20%3D%3D%20false)%20%7B%20%7D%0D%0A
Related Issues:
Tried similar patterns with other types, could not find an error.
The text was updated successfully, but these errors were encountered: