-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type guard and function call #26729
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
Labels
Suggestion
An idea for TypeScript
Too Complex
An issue which adding support for may be too complex for the value it adds
Comments
Copy of #14350 |
Read again related issue, it differs from this |
Simplified example enum Test {
A,
B,
C
}
const test = { prop: Test.A };
function isPropTestA(g: {prop: Test}): g is {prop: Test.A} {
return g.prop === Test.A;
}
if (isPropTestA(test)) {
test.prop === Test.A;
// test.prop === Test.B; = error as expected
} else {
test.prop === Test.A; // no fail, but expected error
test.prop === Test.B;
test.prop === Test.C;
} |
Object types containing a single property of a union type aren't special-cased to be equivalent to an expanded form of themselves. If you add additional properties here you can see why narrowing in the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Suggestion
An idea for TypeScript
Too Complex
An issue which adding support for may be too complex for the value it adds
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms: typeguard type guard function union nested
Code
Expected behavior: no error
Actual behavior: error on call f
Playground Link: https://www.typescriptlang.org/play/index.html#src=enum%20Test%20%7B%0D%0A%20%20%20%20A%2C%0D%0A%20%20%20%20B%2C%0D%0A%20%20%20%20C%0D%0A%7D%3B%0D%0A%0D%0Alet%20a%3A%20%7B%20readonly%20prop%3A%20Test%20%7D%20%3D%20%7B%20prop%3A%20Test.A%20%7D%3B%0D%0A%0D%0Aif%20(a.prop%20%3D%3D%3D%20Test.A)%20%7B%0D%0A%20%20%20%20%2F*%0D%0A%20%20%20%20Argument%20of%20type%20'%7B%20readonly%20prop%3A%20Test%3B%20%7D'%20is%20not%20assignable%20to%20parameter%20of%20type%20'%7B%20readonly%20prop%3A%20Test.A%20%7C%20Test.B%3B%20%7D'.%0D%0A%20%20Types%20of%20property%20'prop'%20are%20incompatible.%0D%0A%20%20%20%20Type%20'Test'%20is%20not%20assignable%20to%20type%20'Test.A%20%7C%20Test.B'.%0D%0A%20%20%20%20%20*%2F%0D%0A%20%20%20%20f(a)%3B%0D%0A%7D%0D%0A%0D%0Afunction%20f(arg%3A%20%7Breadonly%20prop%3A%20Test.A%20%7C%20Test.B%7D)%20%7B%0D%0A%7D
The text was updated successfully, but these errors were encountered: