-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type narrowing bug in empty interfaces #18196
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
Is the proposal to always revert back to the declared type in a false branch when narrowing on truthiness? |
Or perhaps to just not narrow in the false branch? |
Maybe there's something we're misunderstanding about |
I think the bug is that So today: let x: string = ...;
if (!x) {
// x is still `string`
}
let y: number = ...;
if (!y) {
// y is still `number`
} To the extent that ^^^ is the current behavior, I think Ideally, since we have literally types now, I think all false branches should be something like |
Technically you are correct in the case of Nevertheless, I think a better approach here is to use |
@mhegazy at minimum, your union is missing const x: object | string | number | boolean = Symbol(); // does not type check
const x: {} = Symbol(); // type checks It seems problematic for us to need to maintain a union of all of the possible primitive types ourselves. From my perspective, there are a few different things we might be trying to express with the core types:
We typically try to wrangle these types when we start new projects in TypeScript, and we started to formalize our own usage in ts-std. Our We are happy to use whatever unions work to express the most common concepts, and got the idea for We're happy to use |
I would like to fix this issue too, but will need to see what breaks. i am sure some one somewhere depends on this behavior inadvertently. |
Side note: the list of falsy types needs to include: |
@DanielRosenwasser that is correct. However, as far as I can tell, NaN is not a literal type in TypeScript. Is there a trick? |
|
Sure, let's leave To recap:
It seems that there are two options:
It is worth noting that JavaScript is adding a new primitive type (BigInt) that has |
Hey @chancancode and @wycats, now that |
|
Reproduction (turn on
strictNullChecks
to see the red line)The text was updated successfully, but these errors were encountered: