Skip to content

extract union of all keys of objects in a tuple. #39701

@sam-goodwin

Description

@sam-goodwin

TypeScript Version: 3.9.0

Search Terms:
mapped type tuple object keys
union of keys of all objects in a tuple

Code

type A = [{
  b: 'b'
}, {
  a: 'a',
}];

type B = {
  [i in Extract<keyof A, number>]: keyof A[i];
}[Extract<keyof A, number>];

function b(b: B) {
  b // is type never, should be "a" | "b"
}

My thinking:

{ [i in Extract<keyof A, number>]: keyof A[i]; }
// should map to
["a", "b"]

// so indexing that resulting tuple
[Extract<keyof A, number>]

// should return
"a" | "b"

Can be worked around by hard coding expected tuple lengths ...

type Indices = 0 | 1 | 2 ...;
type B = {
  [i in Indices]: keyof A[i];
}[Indices];

Expected behavior:
I would like to extract a union of all the keys found in any of the objects in a tuple. It should return "a" | "b", not never.
Actual behavior:
type B is never.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions