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
1 answer

problem NestedKeyof type with circularly references objects

So I am buildining a library and trying to implement NestedKeyof And I found this. type NestedKeyOf = { [Key in keyof T & (string | number)]: T[Key] extends object ? `${Key}` | `${Key}.${NestedKeyOf}` : `${Key}` }[keyof…
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31
1
vote
1 answer

TypeScript Autocomplete Nested Input Object Key on Nested Return Object

I'm trying to create a factory that takes in a config and returns an object with some autocomplete ability. The functionality is there, but I'm stuck on getting a proper type autocompletion on part of the return value. This is effectively what I'm…
forgo
  • 932
  • 1
  • 7
  • 20
1
vote
1 answer

TS: object type where every possible value must be taken

Given const domain: string[] = ["A", "B", "C"]; how can I declare an object type with keys from domain and values covering whole domain? Some examples for clarification: const obj1 = { // correct type: values { "B", "C", "A" } of obj1 are…
DonFuchs
  • 371
  • 2
  • 14
1
vote
1 answer

Typescript: variable of type "keyof T" used to index T is resolved as union of T values

interface SubProps1 { foo: boolean bar: string baz: T } interface SubProps2 { fooz: boolean barr: string bazd: Array } interface Props { sub1: SubProps1 sub2: SubProps2 } type CSSObjectWithLabel =…
1
vote
1 answer

How to verify that indexed type extends string?

let's say I have function func with 2 generic arguments const func = () => {}; and a type interface Form { a: boolean; b: string; } then I can invoke them like so without any errors func
1
vote
1 answer

How to convert TypeScript Enum key to value and value to key

Context lets say I have the following enum with string values: enum Fruit { Apple = "apple", Orange = "orange", Banana = "banana", Pear = "pear" } the user always inputs the literal string value ("apple", "banana", "orange", "pear") lets…
SagarScript
  • 1,145
  • 11
  • 15
1
vote
2 answers

In Typescript, how can I pass an array of (keyof Object) without having to hardcode it every time? How on earth can this be so complicated?

So I have the following very simple User interface and getUserById() method that retrieves a type-safe user that includes only the specified fields/properties (what is usually called a projection): interface User { _id: string, username:…
cprcrack
  • 17,118
  • 7
  • 88
  • 91
1
vote
1 answer

it is possible to use keyof to update object in typescript?

i want to implementation a function to update object like this. interface Object { name: string age: number } let obj = {} function updateObject(key: keyof Object, value: ???) { obj[key] = value; } It is possible in typescript? and what…
Terriermon
  • 71
  • 6
1
vote
1 answer

How to create a type derived from an union type in typescript

I have the following type type DataTableCol = { title: string; key: K; render?: (value: T[K]) => ReactElement; }; I need to create another type, DataTableRow, derived from it. From type MyData = { name: string; value:…
1
vote
1 answer

How to return a specific type of object property in TypeScript?

Lets say I have a list of flags in an object as below: type Flags = { flag1: string, flag2: string, flag3: boolean, flag4: number } // const myFlags: Flags = { // flag1: 'value 1', // flag2: 'value 1', // flag3: true, // flag4:…
Shnd
  • 1,846
  • 19
  • 35
1
vote
1 answer

How to change the interface key: value: string to [{ [key: string]: string }

I have interface IUser { name: string; surname: string; cityLocation: string; } the value will be const user: IUser = { "name": "John"; "surname": "Smith"; "cityLocation": "LA"; } I have adapter, they have method which receive function…
Jackson
  • 884
  • 2
  • 13
  • 22
1
vote
1 answer

Get type for dynamic field in generic (TypeScript)

Currently I have the following setup. When I pass foo as Column.key, I want the type of the argument value for Column.formatter to be string. For bar it should be number. As is; Typescript expects the type to be all possible types on T. So string or…
yluijten
  • 123
  • 2
  • 8
1
vote
1 answer

Generate union type of all keys from an object with const assertion

I have an object like this (demo in ts playground): const sites = { stack: {url: 'https://stackoverflow.com/'}, google: {url: 'https://www.google.com/'}, azure: {url: 'https://portal.azure.com/'} } as const What I'd like to do is create…
KyleMit
  • 30,350
  • 66
  • 462
  • 664
1
vote
0 answers

how to declare a type that extend a abstract class which has method return type include keyof this in typescript

abstract class AbstractCard { foo = 1 getKeys(): Array { return ['foo'] } } class Card extends AbstractCard { bar = 2 getKeys(): Array { return [...super.getKeys(), 'bar'] } } const logCard = (card:…
Rmaiy
  • 11
  • 3
1
vote
1 answer

Conditionally getting the type of interface property in typescript

Here is my interface: interface Responses { 200:{ foo:number; }; 204:{ bar:number; }; } I can get any of the property type as follow: type TypeFor200 = Responses['200']; However I would like to make it conditional so that if 200…
cyrus-d
  • 749
  • 1
  • 12
  • 29