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

Keyof Type with String autocomplete TS

So I was working on a type and wanted to get recommendation of an object's keys, and if I wanted to use another key, the type could fallback to a string type. I know that the union type with a string basically is the every possible string, so no…
konsalex
  • 425
  • 5
  • 15
2
votes
1 answer

Get the type of a specific key of generic type in TypeScript

I'm struggling to get a complex type functionality out of this updateArray generic function I am building: // Updates an object array at the specified update key with the update value, // if the specified test key matches the test value. //…
fullStackChris
  • 1,300
  • 1
  • 12
  • 24
2
votes
1 answer

Typescript: Work with Child Class in static method

In TypeScript (I used the Playground, version 4.13) , when I inherit from a class, this inside a static method of the parent class seems to refer to the inheriting class: class Parent{ static ID = 0 public id: number static create(){ …
Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44
2
votes
1 answer

Define static indexable object properties in Typescript

I want to code a reusable TypeScript class with an object property, which has indexed, named attributes that can be definied at construction and later modified via their name. These named attributes could be keys of an interface and my intention is…
jacksoor
  • 353
  • 1
  • 12
2
votes
2 answers

Conditionally set attributes in styled-component using props to index on theme object with TypeScript?

Overview I have a next.js (react) TypeScript project that is using styled-components ThemeProvider. I'm trying index on a theme object that stores rem-based font sizes as strings and are indexed by point sizes (integer). The issue that I'm running…
2
votes
0 answers

What's the difference between these two key type argument declarations?

When using a generic wrapper around some kind of known state, I ran into incompatible type errors when casting a child to its parent. The idea being that if Tomato extends Fruit, then Wrapper as Wrapper ought to work. interface…
2
votes
0 answers

Get the key of a specific variable inside a Object to string in typecript/javascript

I want to access an object dynamically like object[variableName] and not object["actual name of variable"]. Okay. lets say I have an object that looks like this: export class Object { "key": string; "name": string; "address": string; } I…
Herman Jansson
  • 164
  • 1
  • 8
2
votes
1 answer

Make generic object optional when mapping over object keys (in keyof)

I have problem with mapping in keyof. I'm trying to map over Routes type, but when I map over the object then it breaks conditional params for Route. type Routes = { '/home': {} '/pages': { pageId: number } } type…
2
votes
1 answer

Using typescript ReturnType with keyof and iterated keys of generic type

I am trying to loop through functions in an object and get their return type to do some filtering as follow: export const StorageActions = { addFile: () => ({ type: 'ADD_FILE' }), deleteFile: () => { return () => { …
cyrus-d
  • 749
  • 1
  • 12
  • 29
2
votes
1 answer

Generic function and infering keyof generic in Typescript

I am trying to remove the second generic from the following function, because (IMHO) it should be possible to infer the K extends keyof T: const normalizeToProps = (...props:K[]) => (list:T[]):{[id:string]:T} => list.reduce( …
Sebastian Sebald
  • 16,130
  • 5
  • 62
  • 66
1
vote
0 answers

Typescript fails to infer key-value pairs from lookup

In the below example I try to map to a list of InnerNamed typed values from a lookup of typed values, (see InnerNamed definition) but the inferred type is way too broad. In the end, the type of resolved should be (InnerNamed<"Cefn", 3> |…
cefn
  • 2,895
  • 19
  • 28
1
vote
1 answer

Typescript - Restrict values of object keys to some possible values, but don't make them mandatory

I've got some types defined as: export const ABC= { A: 'A', B: 'B', C: 'C', } as const; export const DEF = { D: 'D', E: 'E', F: 'F', } as const; export type AbcTypes = (typeof ABC)[keyof typeof ABC]; export type DefTypes = (typeof…
Unapedra
  • 2,043
  • 4
  • 25
  • 42
1
vote
1 answer

Why do we use 'as keyof'?

const isTrue = (arg: T): { arg: T; is: boolean } => { if (Array.isArray(arg) && !arg.length) { return { arg, is: false }; } if (isObject(arg) && !Object.keys(arg as keyof T).length) { return { arg, is: false }; } return {…
Carasidelt
  • 47
  • 5
1
vote
0 answers

How to preserve function overloads when mapping over a Typescript interface

Code Imagine an interface with two function overloads. interface Api { parseNumber(input: string, useBigInt: true): bigint parseNumber(input: string, useBigInt: false): number } How can you map over these functions to create a new interface…
steveluscher
  • 4,144
  • 24
  • 42
1
vote
4 answers

How to use `some` to partially check items in object using TypeScript

I have an object: interface MYInterface { aaa: number; bbb: number; ccc?: number | undefined; } const myObject: MYInterface = { aaa: 0, bbb: 0, ccc: 132, }; I want to check if some keys in this object, satisfy a condition! I'm using…
amdev
  • 6,703
  • 6
  • 42
  • 64
1 2
3
9 10