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

Type of a generic is not narrowed #49676

Closed
valya-smoryakova opened this issue Jun 24, 2022 · 4 comments
Closed

Type of a generic is not narrowed #49676

valya-smoryakova opened this issue Jun 24, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@valya-smoryakova
Copy link

Bug Report

πŸ”Ž Search Terms

discriminated type union, narrowing, generics

πŸ•— Version & Regression Information

  • This changed between versions 3.3.3 and 3.5.1

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A = {
    s: string;
    n: number;
};

// K here may be 's'|'n' or 's' or 'n' (but not never as I understand, as f cannot accept argument of type never in runtime)
function f<K extends keyof A>(key: K): A[K] {
    const k1: typeof key = 's';    // It is completely fine for this line to have an error (as it is outside if check and K may be 'n')

    if (key === 's') {
        // In fact we know that type of x === 's' here.
        // Thus return type here should be A['s'] in other words it should be a string
        // But TypeScript still thinks it is of type A[K]
        
        const k2: typeof key = 's';   // does not work. Here I expect typeof key to be 's'
        const c: A[K]   = 'test';     // does not work. Here I expect A[k] to be string
        const d: A['s'] = 'test';     // works
        
        return 'test';                // does not work. Here I expect 'test' to be asignable to A[K]
    }
    
    if(key === 'n') {
        return 10;                    // the same
    }

    throw new Error();
}

πŸ™ Actual behavior

Typescript ignores the check on the variable typed with a subtype of union type and is unable to narrow type

πŸ™‚ Expected behavior

Typescript narrows the type

My guess is that this behavior may somehow be related to https://devblogs.microsoft.com/typescript/announcing-typescript-3-5/#fixes-to-unsound-writes-to-indexed-access-types

@gerich-home
Copy link

gerich-home commented Jun 24, 2022

Looks similar to #24085

@jcalz
Copy link
Contributor

jcalz commented Jun 25, 2022

Or #33014

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 27, 2022
@RyanCavanaugh
Copy link
Member

#33014 is the best fit for this. Type parameter themselves are not currently narrowable the way the OP implies they should be

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants