Skip to content
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

function's union generic parameter cannot be infered from condition #22594

Closed
zaaack opened this issue Mar 15, 2018 · 2 comments
Closed

function's union generic parameter cannot be infered from condition #22594

zaaack opened this issue Mar 15, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@zaaack
Copy link

zaaack commented Mar 15, 2018

TypeScript Version: 2.7.2

Search Terms:

Code

type Props = {
    a: string
    b: number
}

function test<K extends keyof Props>(key: K, newVal: Props[K], oldVal: Props[K]) {
    if (key === 'a') { // K won't be inferred as 'a'
        shouldBeString(newVal)  // and this line would trigger an error
    } else if (key === 'b') {
        shouldBeNumber(newVal)
    }
}

function shouldBeString(v: string) {

}


function shouldBeNumber(v: string) {

}

Expected behavior:

Generic union should be infered.

Actual behavior:

Not

Playground Link: link
Related Issues:

@zaaack zaaack changed the title Generic function's parameter type cannot infer from condition function's union generic parameter cannot be infered from condition Mar 15, 2018
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 15, 2018
@RyanCavanaugh
Copy link
Member

We don't currently have the ability to track relations from one type guard to another symbol. See also #12184

@zaaack
Copy link
Author

zaaack commented Mar 15, 2018

@RyanCavanaugh Thanks for your fast reply! I see.

@zaaack zaaack closed this as completed Mar 15, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants