Skip to content

Key that must exist on generic type T cannot be used to index type type T #51161

Closed
@jcready

Description

@jcready

Bug Report

🔎 Search Terms

"Cannot be used to index", "generic conditional", "generic extends ternary branch"

Seems similar to #50462 and #50465 but both of those tickets have since been fixed in 4.8.1 and 4.9 respectively while my example is still broken in the playground using 4.8 or nightly (which as of writing this is v4.9.0-dev.20221013)

🕗 Version & Regression Information

  • This changed between versions 4.6.4 and 4.7.4

⏯ Playground Link

Playground link with relevant code

💻 Code

export type AnyOneof = { oneofKind: string; [k: string]: unknown } | { oneofKind: undefined };
export type AnyOneofKind<T extends AnyOneof> = T extends { oneofKind: keyof T }
    ? T['oneofKind'] // error after v4.6.4
    : never;

🙁 Actual behavior

In TS versions greater than v4.6.4 (I've only tested versions available in the playground) the T['oneofKind'] on line 3 is highlighted as an error: Type '"oneofKind"' cannot be used to index type 'T'.(2536).

🙂 Expected behavior

No error on line 3 as "oneofKind" can in fact be used to index into type T since T must have property "oneofKind".

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