Skip to content

Generic type parameter loses defined constraint Β #51009

Closed
@dragomirtitian

Description

@dragomirtitian

Bug Report

πŸ”Ž Search Terms

type parameter constraint

πŸ•— Version & Regression Information

  • This changed between versions 4.9.0-dev.20220914 and 4.9.0-dev.20220915

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export type TypeA = {
    A: 'A',
    B: 'B',
}
export type TypeB = {
    A: 'A',
    B: 'B',
    C: 'C',
}

// Fails to index TypeA with T
type R<T extends keyof TypeA> =
    T extends keyof TypeB ? [TypeA[T], TypeB[T]] : never

// This one works
type R2<T extends PropertyKey> =
    T extends keyof TypeA ?
    T extends keyof TypeB ?
        [TypeA[T], TypeB[T]] : never: never

πŸ™ Actual behavior

In R, TypeA[T] causes an error: Type 'T' cannot be used to index type 'TypeA'.

πŸ™‚ Expected behavior

TypeA[T] should work since T is by definition constrained to be a keyof TypeA

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions