Skip to content

Leverage generic type identificationΒ #48799

Closed
@nugaon

Description

@nugaon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions