72

If I have a type type foo = Array<{ name: string; test: number; }>, would it be possible to get the type of the values within the array, in this case, the interface. I know there is keyof to get the keys, is there something similar for values?

Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
Alex Ngo
  • 755
  • 1
  • 5
  • 4

7 Answers7

113

If you're looking to how to extract { name: string; test: number; } type, you can simply create alias to "item at index":

type Foo = Array<{ name: string; test: number; }>;
type FooItem = Foo[0];

or

type FooItem = Foo[number];
Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
61

Starting with TypeScript 2.8 you can also do this inline with the infer keyword:

type GetElementType<T extends any[]> = T extends (infer U)[] ? U : never;

For example:

// ElementType === string
type ElementType = string[] extends (infer U)[] ? U : never;

The infer keyword is very powerful and can extract any type out of larger type. For example if the type was a function that returns an array:

type FncType = () => [{ name: string }];

// Output === { name: string }
type Output = FncType extends () => (infer U)[] ? U : never;

You can also use the infer keyword in generic types:

type GetArrayReturnType<T> = T extends () => (infer U)[] ? U : never;

// Output === { name: string }
type Output = GetArrayReturnType<() => [{ name: string }]>;
yeerk
  • 2,103
  • 18
  • 16
4

Using the popular utility-types library:

type foo = Array<{ name: string; test: number; }>

type fooElements = ValuesType<foo>
// = { name: string; test: number; }

See https://www.npmjs.com/package/utility-types#valuestypet

4

You can simply get the type of the array using the brackets as follow:

type Foo = Array<{ name: string; test: number; }>
type Bar = Foo[number]; // <- what you want
// then you can use as follow
const bar: Bar = {name:"", test:42};

Bonus:

If your array was a tuple (where elements are typed by index), you can specify which type you want to target using the index within the type's brackets.

type foo = [number, string, ...number[]]; // a tuple or typed array
const barA: foo[0] = 42;
const barB: foo[1] = 'hello';
const barC: foo[2] = 256;
// Using type[number] will generate an union of all the possible types.
// Here below string | number. The following is therefore valid
const barStringOrArray: foo[number] = Math.random() < 0.5 ? 42 : 'hello';
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
3

We can also use an indexed access operator like this:

const someArray = [
    {
        foo: '',
        bar: '',
        baz: ''
    },
    {
        foo: '',
        bar: '',
        baz: ''
    }
];

// indexed access operator
type SomeArray = typeof someArray[number];

There is a write-up on those here: https://www.typescriptlang.org/docs/handbook/advanced-types.html

The second operator is T[K], the indexed access operator.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
2

Despite Aleksey answer, it might be useful to know that if the instance of that generic type exposes at least one member of the type you want to extract, you could use typeof to query the type of that member.

For a generic Array the type can be queried from any array item: enter image description here

Note that line 27 only exists at design time so that will not generate any errors even if arr is empty or undefined at runtime.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • *Currently there is no way to extract the param of a generic type.* Well, for an array, the generic type is the same as type of the member at any index, which you **can** extract, using the "indexed access operator". –  Sep 24 '17 at 06:15
  • @torazaburo by querying the type of an array member you're extracting the member type from an instance of that array type, not from the generic array type itself which is not possible in the current version of TypeScript AFAIK. – cvsguimaraes Sep 24 '17 at 09:57
  • 1
    I think you're confused between the run-time `typeof` result, and the TypeScript type of an array element. –  Sep 24 '17 at 10:08
  • @torazaburo I see what you mean, the member type is being extracted from the type after all even though I can only query its member type from a context that have an instance of `Foo` instead of extracting it "directly" from `Foo`. Also I tried to improve the wording in my answer. Thanks for the heads-up! – cvsguimaraes Sep 24 '17 at 10:15
0

This works with any iterable (including readonly Arrays and tuples):

type ElementType<T extends Iterable<any>> = T extends Iterable<infer E>
  ? E
  : never;

Usage:

ElementType<typeof foo>

Inspired by yeerk

Nick McCurdy
  • 17,658
  • 5
  • 50
  • 82