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
0
votes
2 answers

How use keyof on object shape rather than explicit type

I want to create a type based on the keys of another object in TypeScript. I have managed to do this with type inference. But if I use an explicit type Record then keyof gives me string instead of a union of the actual keys I…
0
votes
3 answers

TypeScript - typesave mapping types of restricted tuple generic

In TypeScript this is not compiling: export interface Generic { } export interface Class { readonly prop: { [P in keyof T]: Generic } } Particularly, the Generic fails with Type 'T[P]' does not…
Reizo
  • 1,374
  • 12
  • 17
0
votes
1 answer

Typescript typeguard on the first generic element of a type

I have an Actions type that need to be mapped to a set of callbacks in the actionMap variable. However, I'm unsure on how to enforce type of the first generic element of the Action type. Clearly key in keyof Actions isn't doing the trick here. What…
0
votes
1 answer

Correct hashmap typing as function argument in TypeScript

import React from 'react'; import styled from 'styled-components'; const IconWrapper = styled.Text` font-family: MyFont; `; const glyphs = { 'logo': '\ue94e', 'minus': '\ue900', 'plus': '\ue901', ... }; interface IconProps { glyph:…
xercool
  • 883
  • 1
  • 8
  • 24
0
votes
1 answer

Typescript keyof overlap of Partial

I would like to type a DB insert function that allows defaults: (I am using SetOptional of type-fest) function defaultInsert>(data: SetOptional, defaults: D) { What I am trying to say with the SetOptional is that the…
0
votes
1 answer

How to create object literal with a property named as `keyof` type

I cannot even find proper words to formulate the question. Here is a simplified code of what I want to achieve. class Test { public create(id: T[TId]): T { return { [TId]: id, // Error here. I want to set property…
AKd
  • 501
  • 4
  • 17
0
votes
1 answer

TypeScript alias for asserting value type for keyof

Having trouble discovering if you can assert the value type of a property with a type alias. Here's my example: type Primitive = string | number | boolean; function comparePrimitives(a:Primitive, b:Primitive): number { /*...*/ } …
Oren Ferrari
  • 89
  • 1
  • 10
0
votes
2 answers

Typescript type safe update of property by string property name

I'm needing to update the value of a property of a class by a string property name. I started off by making sure the property name was valid via this method: export class ClientDTO { ... static isValidPropertyName(name: string): name is…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
0
votes
1 answer

Can't pass argument to `forEach`

I would like to ask why the compiler doesn't let me use the callback function to forEach although as far as I know "a" | "b" | "c" | "d" is a subtype of string. Please check out the error message after the code block. I guess I could solve this…
Take
  • 324
  • 3
  • 10
0
votes
1 answer

Unable to understand the semantics of keyof in TypeScript

I have the following simple example of where K extends keyof T is declared, but the result of the return type inferred for findMember is different, I really don't understand why there is such a difference. I also created a link to TypeScript…
Trash Can
  • 6,608
  • 5
  • 24
  • 38
0
votes
1 answer

How to link keyof types within an interface

Not sure if this is possible, but it's related to keyof which I'm having a hard time wrapping my mind around... I currently have this interface: interface Column { header: string; field: keyof T; format?: (value: any) => string; } First…
Svish
  • 152,914
  • 173
  • 462
  • 620
0
votes
1 answer

Enforce boundaries of key in an index

I am trying to have a function that takes an index as parameter where the key is limited to a key of T function aliasSet(values: {[x:keyof T]:string}) //compiler error: An index signature parameter type must be 'string' or 'number' Is there…
Yooz
  • 2,506
  • 21
  • 31
0
votes
1 answer

Get parameters from generic functions' declaration - TypeScript 3.3

I don't know how to get type of parameters of functions which are declared in interface. I need to have proper type checking of them.. Probably I need to use: Parameters class from TypeScript version: 3.3:…
Horyzont
  • 59
  • 1
  • 4
0
votes
1 answer

Keyof different behavior using constructor assignment

I'm experiencing a different behavior of the "keyof" using the constructor assignement... Here is the code class Class1 { constructor(private a: number, private b: string) { } method1() { console.log("method1"); } } class Class2 { a:…
user2010955
  • 3,871
  • 7
  • 34
  • 53
0
votes
2 answers

Enforcing that an array within an object literal can only contain keys of the outer object

I have the following Interface definitions. interface IComponents { root: IComponent, [key: string]: IComponent, } interface IComponent { type: string, children?: Array; } I want that the "children" properties accept…
1 2 3
9
10