-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Runtime Validation With Variables Fails #48241
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
This is working as intended. See #44730.
|
@MartinJohns Got it, that's super strange and not what I would have expected, as it seems similar to the f6 function in #44730, but I guess the compiler doesn't know which type to choose still/can't pick one? |
Look closer:
|
@fatcerberus I meant closer in terms of removing the obj reassignment in-function. IE: If it was like this: function f6(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) {
const isFoo = obj.kind === 'foo';
if (isFoo) {
obj.foo; // Error, no narrowing because 'obj' is assigned in function body
}
} It seems that because obj has two distinct definitions, this is why it works? So in my example, if test was defined as: interface test {
val1: string;
} | {
val1: undefined;
} Then everything works fine, which is weird to me was my point. |
Ah, that's a bit tricky, but I suspect that works because |
That'd be my best guess as well. |
We're quite a bit stricter about when to apply indirect narrowing as opposed to indirect narrowing. If you mark it as |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
typescript validation variable boolean failure
🕗 Version & Regression Information
Version 4.6.2 and below all seem to exhibit the same issue. Also confirmed most recent nightly build as of this filing exhibits the same issue.
This is the behavior in every version I tried, and I reviewed the FAQ for entries about TypeScript type validation
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Receive the following error for type validation:
🙂 Expected behavior
Type validation from the boolean expression should work.
The text was updated successfully, but these errors were encountered: