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
1 answer
How to change type of index expression? ie: c[k]
My Code
import * as R from 'ramda';
import { ILPAsset } from 'shared/types/models';
interface TextMatchFunction {
(part: string, typed: string): boolean;
}
const textMatch: TextMatchFunction = (part: string, typed: string) => typed.search(part)…

Leon Gaban
- 36,509
- 115
- 332
- 529
0
votes
0 answers
keyof returning different types with index signature
I have some confusion about how keyof works when I have an interface with an index signature.
As an example, if I have an interface as follows:
interface InterfaceOne {
[id: string]: InterfaceOne[keyof InterfaceOne];
a: SomeTypeOne;
b:…

sam
- 2,469
- 8
- 37
- 57
0
votes
1 answer
In TypeScript how do I declare a generic with 2 arguments, where the first is a key of the second that returns a specific type?
I'm looking to have a generic class like this one:
export abstract class Foo {
bar(T t) {
const s: string = t[ID];
}
}
Obviously the code above can't infer the type of t[ID] and we're getting an implicit…

ronif
- 695
- 1
- 6
- 14
0
votes
1 answer
Partial only works with inline values
Why does the code below behave like it does? Is it a bug in the TypeScript compiler or missing feature?
class MyType {
constructor(public readonly value1: string, public readonly value2: number) {
}
}
function myFunction(props:…

Martin Ingvar Kofoed Jensen
- 4,241
- 2
- 29
- 31
0
votes
1 answer
Typescript + keyof. Declaring an object transformation
im trying to understand keyof.
I want to describe a function which receives an object { a : 1, b : 'anything'} and should return something like { a: true , b: false } (same keys, but always boolean values).
But when I write (example)
function…

wkrueger
- 1,281
- 11
- 21
-1
votes
1 answer
Constrain key so it can only reference keys of TItem that are number or Date
I am trying to constraint key so it can only reference keys of TItem that are number or Date.
public static indexOfMin(array: Array, key?: keyof TItem): number {
let min: number =…

Mark Golding
- 3
- 2