Skip to content

Inferring a key within a string inconsistently yields too wide a unionΒ #57126

Closed as not planned

Description

πŸ”Ž Search Terms

infer, keyof, string, interpolation, union, all keys, too big, too wide

πŸ•— Version & Regression Information

Syntax not supported before 4.7
Similar behavior on all versions upwards

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.3&ssl=6&ssc=1&pln=21&pc=47#code/PTAEEkDsDMFMCd4EtIHNQENQGtYE9QB3JAFwAsVNQBnE5NUFAYwHtJqlbZISAbAvEli8AJtVAkWLIkhGwqAV0hI2AWABQGkDTw8MAD1CQWJGgoAO5lvBKwRoAEaxo1+QBYAdAHYPbrWAcFdGh4FgBbCTIEeTVNdRI8c3kAEQAGUABeUABvDVBMAC5QAHJU4oAaPMci4oBGCqqmGoAmYo0AXwBuDQ0EpNBk2sycqowMGrLK9XyHGvqp-KaS1o7uuL75ABVYWgAeZIwSDHLQTYA+Yc3QWH1bSDFQAAMAEmyUOHhQAGlr2+4H3B4FjQAaHLAAMhodBQqHar1o9Fhj1AAH5vqAitAMLxqLA1r1Elt0lltns0idihgABo04pnTr5fLaSlteKE05DEk7Ej7WoUsY0ql0hlMsDFBzFAA+xSYUspGFZQA

πŸ’» Code

type D0 = {
  a: '0',
  b: '1',
  c: '2'
};

type D1 = {
  aa: '0',
  b: '1',
  c: '2'
};

type Test<Data, T> = T extends `${infer K extends keyof Data & string}${string}` ? K : false;

type T0 = Test<D0, 'aXXX'>;    // 'a'
type T1 = Test<D1, 'aaXXX'>;   // 'b'|'c'|'aa'

πŸ™ Actual behavior

T1 is the union of all keys of D1 while only one fits within the pattern.
The extra keys are irrelevant.
The inference is not helpful.

This behavior seems inconsistent since T0 gives the expected key.

πŸ™‚ Expected behavior

T1 = 'aa'

Additional information about the issue

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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