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
0
votes
0 answers
specify type of keyof target
Is there any way to specify the type of object a keyof is pointing to?
So that, for example:
type MyType = { a: string, b: number };
let myKey: keyof MyType = "a"; // Valid
myKey = "b"; // Invalid
And then, when you use:
let myVar: MyType =…

Trevortni
- 688
- 8
- 23
0
votes
1 answer
How to use TypeScript "keyof" to create interface to generic API interface?
I am having trouble using TypeScript's type system to model a generic interface to call from Lua into TypeScript. For this to work, the developer must define a TypeScript interface defining the TypeScript methods that Lua can call into. The…

deltamind106
- 638
- 7
- 19
0
votes
2 answers
Typescript interface problem: how to get the type like t when we have a const like list
const list = [
{
label: "phone",
defaultValue: "1203981209",
},
{
label: "isMan",
defaultValue: false,
},
{
label: "age",
defaultValue: 22,
},
];
type t = {
phone: string;
isMan: boolean;
age:…

user8154168
- 23
- 6
0
votes
1 answer
Cannot use index access type with T[Key extends keyof T]
I found myself with very often working with arrays like
[
{key1: val1, key2: value2, key3: val3},
{key1: val1, key2: value2, key3: val3},
{key1: val1, key2: value2, key3: val3}]
where I want to transform this into a dictionary/map,…

friartuck
- 2,954
- 4
- 33
- 67
0
votes
1 answer
Can't use keyof to assign to a field
I would like some explanation about this error I encounter:
interface Test {
a: number;
b: string;
c: string;
}
function f(obj1: Test, obj2: Test, field: keyof Test) {
obj1[field] = obj2[field]; // Error: Type 'string | number' is…

Charles
- 125
- 1
- 6
0
votes
1 answer
TS keyof typeof
I do not understand what the problem is with this code.
Here is my code:
export type SportsTypes = keyof typeof SportsIcons
export const sports: SportsTypes[] = Object.keys(SportsIcons);
I am typing my variable to the keys of an object.
But, when…

wynx
- 243
- 2
- 14
0
votes
1 answer
Get all keys from array of (different) objects type
I have a type FooBar, that contains objects with different props. I want to get the keys from all objects. I thought that could be obtained by using keyof FooBar[number]; but it only returns the common keys.
type FooBar = [
{
foo: "hello";
…

Richard Fernandez
- 558
- 1
- 6
- 18
0
votes
2 answers
What is the index type of const object in TypeScript?
I would like to define a type based on values of an Object.
For example,
const foo = {
'a': ['1', '2', '3', '4'],
'b': ['5', '6', '7', '8'],
'c': ['9', '10', '11', '12'],
};
type RandomType = typeof foo[Readonly][number]; //…

Nelson Luk
- 171
- 1
- 11
0
votes
0 answers
Constraining a function argument to never to eliminate invalid code paths where a mapped type doesn't satisfy a constraint
I put together a simple Store implementation which wraps an immutable javascript 'root' item as state.
A partition operation allows a child Store to be derived from a parent store using a key. The new Store's root state is the child of the original…

cefn
- 2,895
- 19
- 28
0
votes
1 answer
Typescript: keyof type issue
For example, if I have this User model:
export interface User {
firstName: string;
lastName: string;
age: number;
password: string;
id: number;
}
and try to run this code:
let user = {
firstName: 'string',
lastName: 'string',
age:…

Ashot Aleqsanyan
- 4,252
- 1
- 15
- 27
0
votes
1 answer
In method use keyof to type parameter
Suppose you have the following class:
class Foo {
protected callbacks: Callbacks = {};
bar(key: string) {
const callback = this.callbacks[key];
// do stuff with callback
}
...
}
The callback property can be changed (later) by…

Roelof Hoeksema
- 23
- 6
0
votes
1 answer
How to have required and optional type parameters in a function and still have working type inference for recod lookup?
How to make a required type parameter Y in a function work together with another type parameter K where the latter is only used as a key in a record R = {a: 'aaa', b: 'bbb'} lookup and is not supposed to be explicitly provided? The type parameter K…

Peter Hudec
- 2,462
- 3
- 22
- 29
0
votes
0 answers
typescript fail to catch incompatible generic type error
I want to create a function that can handle feature object of generic type. The code is as follow
interface ComplexTypeMap {
featureA: { name: string },
featureB: { age: number }
}
function testComplexTypeMap

Fred Yang
- 2,521
- 3
- 21
- 29
0
votes
1 answer
Typescript generics don't resolve concrete types as I would like them to be
I have a class that keeps a list of item, but instead of keeping them in a flat array, it keeps them in an object map of which each property represents a group of items. Like if we'd have a list of cars and we'd group them per manufacturer.
//…

Robert Koritnik
- 103,639
- 52
- 277
- 404
0
votes
1 answer
What is the proper way to type a method that receives an object and updates properties?
My question is about my update method. As you can see below, it can receive an object(newState), and updates the class instance properties using Object.assign(). I need to tell TS that it should only accept:
An object
Properties that are keys of…

Jordan
- 194
- 1
- 12