Questions tagged [keyof]

This is a typing construct in Typescript (2.1+) which allows static typing for dynamically named properties. Basically, the new type will be a union of all available key properties in an object.

141 questions
314
votes
5 answers

What does "keyof typeof" mean in TypeScript?

Explain to me what keyof typeof means in TypeScript Example: enum ColorsEnum { white = '#ffffff', black = '#000000', } type Colors = keyof typeof ColorsEnum; The last row is equivalent to: type Colors = "white" | "black" But how does it…
user1283776
  • 19,640
  • 49
  • 136
  • 276
154
votes
1 answer

In TypeScript, what do "extends keyof" and "in keyof" mean?

In TypeScript, some types are defined using extends keyof or in keyof. I have tried to understand what they mean, but so far I didn't succeed. What I got is that keyof alone returns a union type which has all the names as possible values that are…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
69
votes
3 answers

In TypeScript, how to get the keys of an object type whose values are of a given type?

I've been trying to create a type that consists of the keys of type T whose values are strings. In pseudocode it would be keyof T where T[P] is a string. The only way I can think of doing this is in two steps: // a mapped type that filters out…
Aron
  • 8,696
  • 6
  • 33
  • 59
21
votes
3 answers

Typescript: derive union type from array of objects

I would like to declare a type-enforced array of items and be able to derive a union type from it. This pattern works if you do not explicitly give a type to the items in the array. I am not sure how to best explain it so here is an example: EXAMPLE…
Ben
  • 263
  • 1
  • 2
  • 6
19
votes
2 answers

TypeScript: How to deal with generic types and the keyof operator

I try to write a generic function which assembles update data for database updates. Passed arguments: record to be updated property key a new array item Even though I restrict the key's type using keyof R, I cannot assign a new object with that…
ideaboxer
  • 3,863
  • 8
  • 43
  • 62
18
votes
2 answers

Typescript: constrain argument of function to be a key of an object associated with a value of a particular type

Is there a way to make the following type check? function getNumberFromObject(obj: T, key: keyof T): number { return obj[key] // ERROR: obj[key] might not be a number } I want to specify that key should not only be a key of T, but a key with a…
Lionel Tay
  • 1,274
  • 2
  • 16
  • 28
13
votes
2 answers

How to avoid dynamic keyof object assign error in TypeScript

Let's say we have TypeScript code that looks like: type User = { id: number, name: string, } let user1: User = {id: 123, name: "Hello"}; let user2: User = {id: 456, name: "World"}; let keys: (keyof User)[] = ["id", "name"]; for (let key of…
Vaibhav K
  • 388
  • 1
  • 6
13
votes
2 answers

Make a generic type Array require all keys of T

I would like to declare a type that requires all the keys of a given type T to be included in an array, e.g.: checkKeys(arr: Array): void { // do something } interface MyType { id: string; value: number; } Currently if a call…
don
  • 4,113
  • 13
  • 45
  • 70
12
votes
1 answer

Expected 3 type arguments but got 1 but it should infer 2 types

I wondering how to correctly infer 2th and 3th template of my function suppose a simple interface interface ISome { a: string; b?: { c: string; }; } The following works: function pathBuilder< K1 extends keyof ISome, K2…
Lorenzo Delana
  • 532
  • 5
  • 11
7
votes
1 answer

Get string literal types from Array of objects

So from: export interface Category{ val: string; icon: string } const categoryArray: Category[] = [ { val: 'business', icon: 'store' }, { val: 'media', icon: 'video' }, { val: 'people', icon: 'account' }, …
TrySpace
  • 2,233
  • 8
  • 35
  • 62
7
votes
1 answer

TypeScript keyof index type is too wide

Apologies, I'm sure this has been answered somewhere, but I'm not sure what to google. Please do edit my question if the terms in the title are wrong. I have something like this: type RowData = Record & {id: string}; type…
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
6
votes
1 answer

List private property names of the class

I need to use some subset of class properties names as values in a map to use inside of the class. In following example I've replaced map by array. The problem is that if property is marked private it's not listed in keyof list. How can I specify…
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
6
votes
2 answers

Typescript: Infer type of nested keyof Properties

I want to define an Array type that must contain a chain of nested property names of a given type. Let's say I have a type: type Foo = { outer: { inner: any; } } Now I want to define an Array type with 2…
Reducer
  • 670
  • 7
  • 14
5
votes
2 answers

Typescript type safe string with dot notation for query nested object

I am working with Typescript and firebase and I have a small abstraction layer with this function to search for a unique document base on its field name and its value. where(fieldName: K, operator:…
5
votes
2 answers

How can I specify that Typescript generic T[K] is number type?

I have a simple Typescript function like this: function getProperty(obj: T, key: K): number { return obj[key]; // This line is not compiling. // Typescript will yell: "Type 'T[K]' is not assignable to type 'number'." } My…
Joseph
  • 3,974
  • 7
  • 34
  • 67
1
2 3
9 10