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
1
vote
1 answer
Typesafe assign to object at key from string
I'm trying to figure out how to create a typescript setter function which takes in an object, a key of that object, and a value to assign at that key.
Here's what I have so far:
const setter = (obj: T, key: K, value) => {
…

Mike Cluck
- 31,869
- 13
- 80
- 91
1
vote
2 answers
Only allow keyof a certain type
I have this simple function for sorting objects by date. But currently I have to check if the field actually is a date, before doing the comparison. Is there a way to limit K to only allow keys that are of a certain type, in this case Date?
const…

Svish
- 152,914
- 173
- 462
- 620
1
vote
1 answer
How can I get an index type from the keys of a typed object?
I have used the keyof operator before to get the keys of an object but I'm stuck on this simple code.
interface Definition { name: string; visible: boolean; itemIds: number[]; }
const Definitions: { [key: string]: Definition } =
{
line1: {…

Leonor
- 357
- 2
- 8
1
vote
1 answer
Typescript: for (let key in obj), `key` is `Extract` instead of just `keyof T`, why?
I want to iterate over an object, when I use for-in, I notice a very weird thing:
function forIn(obj: T): void {
for (let key in obj) {
// key's type is `Extract` instead of just `keyof T`
}
}
My VSCode…

Joseph
- 3,974
- 7
- 34
- 67
1
vote
1 answer
Union type of keyof and Lookup Types pairs
From types like these:
type I1 = {
a: number;
b: boolean;
};
type I2 = {
x: string;
y: number;
};
I want to get types like these:
type O1 =
| {
name: "a";
value: number;
}
| {
name: "b";
value: boolean;
…

pomber
- 23,132
- 10
- 81
- 94
1
vote
1 answer
Typescript's keyof with array of objects
I'm very new to typescript and having a hard time finding some extensive documentation on keyof. If you have
export interface someClassProps {
/*
Lets assume T look is an object that looks something like:
T =…

Grant Williams
- 1,469
- 1
- 12
- 24
1
vote
1 answer
define Map with T[K]
I'm currently in the process of transitioning from JS to TS and can't quite get my head around keyof. I found https://github.com/kimamula/TypeScript-definition-of-EventEmitter-with-keyof and now I'm trying to implement this. All went fine until I…

Noel Heesen
- 223
- 1
- 4
- 14
1
vote
2 answers
Generic keyof and property getter function
I'm trying to observe properties of objects in arrays. To make it type safe I'm using a getter function to get to the child object of the array objects (if necessary) that contain the actual property to be observed.
The propertyName / key must a…

Laurens
- 91
- 8
0
votes
1 answer
typescript deep keyof when field is an option
I create an function to select and keyof of deep object.
but I don't know why this keyof on my object is not work when it is an option.
type Database = {
User?: {
id: string
name: string
},
Post?: { // if i remove ? it…

Mr.Trieu
- 414
- 2
- 6
- 16
0
votes
1 answer
Calling functions by using indexers created by typeof keyof
I'm trying to understand in what way can I call static functions dynamically, accessing the functions' class using an indexer.
Consider this simple example:
Comparer.ts defines static functions:
class Comparer {
static strings(a: string, b:…

OfirD
- 9,442
- 5
- 47
- 90
0
votes
0 answers
How to ensure all keys in object are strings?
Playground
How do I get this key to be a string when it also has to be a keyof T?
function hello (object: T, key: keyof T) {
hasToBeString(key)
// ^?
}
const hasToBeString = (v: string) => {
…

ThomasReggi
- 55,053
- 85
- 237
- 424
0
votes
0 answers
TS generic type parameters on constructor method
TypeScript allows marking generics at the method level to constrain the type of one parameter based on the type of another parameter.
class Publisher {
publish(channel: K, payload: Channels[K]) { }
}
However, it seems…

Michael Moreno
- 947
- 1
- 7
- 24
0
votes
2 answers
How to force function parameters to match key and type of object?
I would like to force the 2 parameters of a function to respect the name and the type of an object. So I am looking for the XXX type of the val parameter to be the type of the T[key]:
class I {}
class W {
field

Javaddict
- 483
- 1
- 4
- 15
0
votes
0 answers
how to create keyof of multiType with string appender
I am having trouble with building keyof with mutitype
See below to know the issue
export type NonFunctionPropertyNames = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
export type JoinNoneFunctionPropertyNames

Alen.Toma
- 4,684
- 2
- 14
- 31
0
votes
1 answer
Is there any way to reassign the type of property of a class according to passing argument type?
I am trying to dynamically assign type of a static property in a class.
And use the typeof, keyof and other features of typescript further.
Here is my code
class Neith {
static config: any;
static Loader(conf: T) {
…

Subhasish Biswasray
- 18
- 4