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
1
vote
2 answers

typescript 'keyof' change the origin property optional?

Define an interface like this interface Props { a?: string b?: string c?: string d: string } When I do something like this, it is ok type Test1 = { [P in keyof Props]: Props[P] } const a1: Test1 = { d: '1' } but, like this, I got some…
Ben
  • 73
  • 1
  • 5
1
vote
2 answers

Recursive type definition with mapped and conditional types

I'm trying to come up with a way to have better type safety with TypeORM. The following are some example TypeORM entity definitions. import { BaseEntity, Entity, Column, ManyToMany, JoinTable, ManyToOne, OneToMany } from 'typeorm'; @Entity() class…
Utku
  • 2,025
  • 22
  • 42
1
vote
1 answer

Typing for key by generic type lookup

I'm trying to get strong typings like this: enum Schema { content = 'content', interaction = 'interaction', } type ContentData = { content: string; }; type Data = Record & { [Schema.content]: ContentData; } type Event
Sergej
  • 1,082
  • 11
  • 27
1
vote
0 answers

Make typescript parameter keyof K and typeof FormGroup

I want to pass in an object which is key of a class K and has the type of or extends FormGroup like: public async myMethod( ... formGroupName: keyof K & typeof FormGroup) { } So wenn I call it then, the keyof K seems to work as I…
jo-chris
  • 362
  • 6
  • 13
1
vote
0 answers

typescript loop object rewrite self property

function resetForm () { // const INITIALFORM = { // url: '', // device: 'mobile', // dimension: ['time', 'page'], // } for (const key in INITIALFORM) { type KeyType = keyof typeof INITIALFORM const…
mihuar
  • 13
  • 3
1
vote
1 answer

TypeScript: keyof unexpected behavior

class Target { prop1 = "321"; } const target = new Target() class Target2 { prop2 = "123"; } const target2 = new Target2() type TValue = { target: V; key: keyof V; } class Case[]> { …
Just Alex
  • 183
  • 1
  • 12
1
vote
2 answers

Typesript argument depends on another argument with a default value

I am trying to make a function in typescript where the second, optional argument, is a key of the first argument. Without the optional argument, the function I want looks like function getVal(obj: T, key: keyof T) { return…
Jason Siefken
  • 737
  • 7
  • 12
1
vote
1 answer

How to dynamically assign values to object properties in Typescript?

I have these two types: type ArbitraryPropertyName = keyof typeof arbitrary; type D = { [k in ArbitraryPropertyName]: any }; Now I want to declare a variable of type D, but I don't want to assign each arbitraryPropertyName one by one, like…
1
vote
0 answers

TypeScript: get the type of the value of generic object property with keyof T

I have looked at similar questions here here here and here that so far have not helped resolve my issue. Apologies if there is overlap that I was not able to extract. I'm having a really tough time explaining my issue, so here goes my best…
BradStell
  • 371
  • 3
  • 17
1
vote
1 answer

In TypeScript, how can I infer my arguments and impose a constraint that all items be keyof T, where T is the generic type for the function?

My goal here is to create a function named: getFields. This function has a generic and a parameter ...fields: Array. I would like this function to return a function which when given an object of type will return a reduced object…
1
vote
2 answers

is there a way to describe a kind of "keyfor" in typescript?

is there any way to express that a key-property for an object should be limited by related value? Something equivalent to keyof but for the value related to given key? const objectAction = (obj: T): void => obj; const…
m. maylon
  • 13
  • 2
1
vote
2 answers

Typescript function where object has a property of type string determined by another parameter

I want to create a TypeScript function that takes an object and a property within that object, for which the value is a string. Using works to make sure only keys of T are allowed as values for the property, but I cannot seem…
JHH
  • 8,567
  • 8
  • 47
  • 91
1
vote
1 answer

How to represent group of data as a generic Typescript interface

In short: How can I declare, using Typescript type's system, a generic interface which takes as type parameters a type Gof a group (all the properties that belong to a group) a type I of an item in the group (all the properties of an item, of which…
MaxAxeHax
  • 423
  • 7
  • 14
1
vote
1 answer

Why does this TS implementation of "prop" work but mine does not?

I was looking through some blogs about how to use the keyof feature of typescript to implement a classic function in Ramda/Underscore called prop which returns the value corresponding to a given key on a certain…
Luís Hendrix
  • 156
  • 2
  • 6
1
vote
3 answers

typescript: enforcing existing object keys

I would like dropdownAttributes to limited to attributes on DropDownItem interface. interface DropDownItem { [key: string]: any; } interface Props { dropdownList: DropDownItem[]; dropdownAttributes: string[]; } if DropDownItem did now…
pethel
  • 5,397
  • 12
  • 55
  • 86