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.
Questions tagged [keyof]
141 questions
5
votes
1 answer
Typescript check if type A === type B | type C
In one file I have something like this:
export const _all = {
a: '',
b: '',
c: '',
d: '',
e: '',
f: '',
}
type AllKeysType = typeof _all;
export type AllKey = keyof AllKeysType;
In another file I have something like this:
export const…

Marek Krzeminski
- 1,308
- 3
- 15
- 40
4
votes
1 answer
Type 'any' is not assignable to type 'never' when trying to set Object property with variable which is keyof ObjectType
In the code below typscript compiler shows error in update method, saying that 'any' is not assignable to type 'never'. I noticed that keyof type not working when the type contains boolean mixed with other types. How can I make it compile having…

Romikas
- 73
- 1
- 6
4
votes
1 answer
Get Keys of a Type using Keyof Type Operator
I have this Identity interface
export interface Identity {
username: string;
name: string;
surname: string;
roles: string[];
}
and I want to get an array of Strings at runtime with all its keys.
const keys: string[] = ["username",…

1Z10
- 2,801
- 7
- 33
- 82
4
votes
2 answers
How to type-hint a getter to only allow keys of dict?
I wonder how to make this getter more type-safe:
VALUES = {
'1': 'One',
'2': 'Two',
'3': 'Three'
}
def get(key : str) -> str:
return VALUES[key]
Instead of the type str I would love to have a keyof VALUES and type(VALUES[key])…

Henry Ruhs
- 1,533
- 1
- 20
- 32
4
votes
1 answer
Typescript parameters - a generic array of objects and array of object's keys (partial)
I want to have a method that accepts an array of objects and an array of some of the objects keys. The method will return an array of arrays of object values but only of the selected keys.
data:
[
{"firstName": "Jane", "lastName": "Doe"},
…

CrossTheDev
- 151
- 1
- 11
4
votes
1 answer
TypeScript array type transform with keyof like method
I'm trying to achieve something like clone of one array type with specified value types as array with functions which return specified values.
Say we have array tuple like:
[string, number]
And what I want is to get generated type from it like:
[()…

ThaFog
- 493
- 3
- 15
4
votes
3 answers
Merging types in typescript (adding keys to an existing keyof type)
If I have a typescript type consisting of keys:
const anObject = {value1: '1', value2: '2', value3: '3'}
type objectKeys = keyof typeof anObject
and then I wish to add keys to that type, while retaining the current keys, how do I go about doing…

laramie
- 77
- 1
- 6
3
votes
1 answer
How to distinguish different functions signature with conditional type checks?
I'd like to be distinguish the following function types in conditional type checks:
type SyncFn = () => void;
type AsyncFn = (data: number) => Promise;
type SyncFnWithArg = (data: number) => void;
So I can then use the KeyOfType that @Titian…

awdk
- 1,477
- 12
- 17
3
votes
2 answers
Typescript access and assign object property by dynamic key
I am trying to assign a value from an object of type TestInterface to another object of type TestInterface by accessing it with a dynamic key of type keyof TestInterface. However typescript seems to forget that this key is the same on both instances…

Robert Haslinger
- 155
- 1
- 17
3
votes
2 answers
How to set default value to param that has type keyof (from an explicit type) in Typescript
I am having trouble in Typescript passing default values to a function similar to pick from lodash.
The function accepts an object of known (non-generic) interface and a set of keys to pick and return from the object.
Regular (no default params)…

mitsos1os
- 2,170
- 1
- 20
- 34
3
votes
1 answer
Why can't Typescript use keyof type of a generic in an assignment and generalizes it to string?
I came across a curious Typescript assignment error and I'm trying to understand more about why it doesn't work. For reference I'm using TS 3.9.2.
The code:
function test(a: U, k: keyof U) {
const x: Partial = { [k]: a[k]…

Gianluca Venturini
- 369
- 2
- 10
2
votes
1 answer
Inferring Generic keys when editing Record
This code example is a function for terse immutable editing of a record to set boolean values.
The function should accept a record of boolean values and a list of matching keys. It should return a new record which has all those keys set to true…

cefn
- 2,895
- 19
- 28
2
votes
1 answer
Is there a way in Typescript to instantiate a Record by assignment in a generic function where: T extends an object-type and K = keyof T?
What I'm trying to do is this:
interface A {
a: number
b: number
}
function f() {
const x: Partial> = {a: 'generz'}
console.log(x)
}
But, when compiling (using tsc v4.9.3) I get this error…

pintarj
- 75
- 10
2
votes
2 answers
Type reduce array return objects bu id
Usually when we are fetching we get an array with complex objects with ids. Like this:
const arrayOfObjects = [
{
id: '56gfl3Y23',
brand: 'Bosch',
price: 1234,
specs: ['feature1', 'feature2', 'reature3'],
// ... other fields
…

JS_User
- 31
- 3