Skip to content

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

Closed
maximelkin opened this issue Aug 29, 2018 · 4 comments
Closed

Type guard and function call #26729

maximelkin opened this issue Aug 29, 2018 · 4 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@maximelkin
Copy link

maximelkin commented Aug 29, 2018

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms: typeguard type guard function union nested

Code

enum Test {
    A,
    B,
    C
};

let a: { readonly prop: Test } = { prop: Test.A };

if (a.prop === Test.A) {
    /* Error:
    Argument of type '{ readonly prop: Test; }' is not assignable to parameter of type '{ readonly prop: Test.A | Test.B; }'.
  Types of property 'prop' are incompatible.
    Type 'Test' is not assignable to type 'Test.A | Test.B'.
     */
    f(a);
}

function f(arg: {readonly prop: Test.A | Test.B}) {
}

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

@maximelkin
Copy link
Author

Copy of #14350

@maximelkin maximelkin reopened this Aug 29, 2018
@maximelkin
Copy link
Author

Read again related issue, it differs from this

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Aug 29, 2018
@RyanCavanaugh
Copy link
Member

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 else branch isn't sound in the multi-property case.

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
Projects
None yet
Development

No branches or pull requests

2 participants