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

Leverage generic type identification #48799

Closed
nugaon opened this issue Apr 21, 2022 · 2 comments
Closed

Leverage generic type identification #48799

nugaon opened this issue Apr 21, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@nugaon
Copy link

nugaon commented Apr 21, 2022

Bug Report

πŸ”Ž Search Terms

inferring, infer, generic type, generic, user-defined type guard, conditional type

πŸ•— Version & Regression Information

  • This is a bug in all versions of TypeScript

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type MainT = 'john' | 'doe'
type ValueT<T extends MainT> = T extends 'john' ? string : T extends 'doe' ? number : never
type ReturnT<T extends MainT> = T extends 'john' ? Uint32Array : T extends 'doe' ? Uint8Array : never

function isMainT<T extends MainT>(value: unknown, mainType: T): value is T {
    return value === mainType
}

function test<T extends MainT>(key: T, value: ValueT<T>): ReturnT<T> {
    if(isMainT(key, 'john')) {
        const n = value.split('') // ValueT<T> is not inferred when T is known by using typeguard on `key`
        return new Uint32Array() // ReturnT<T> is not inferred when T is known
    }
}

πŸ™ Actual behavior

When the type of the main generic type (MaintT) is identified by a User-Defined Type Guard function, the other conditional types based on this main type are not identified and TS keeps it generic.

πŸ™‚ Expected behavior

ReturnT<T> and ValueT<T> became inferred after T has been identified

@andrewbranch
Copy link
Member

Duplicate of #33912 and #35873

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Apr 21, 2022
@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

3 participants