keyof Questions
3
I wonder how to make this getter more type-safe:
VALUES = {
'1': 'One',
'2': 'Two',
'3': 'Three'
}
def get(key : str) -> str:
return VALUES[key]
Instead of the type str I would love to ha...
Mezcaline asked 10/8, 2021 at 21:17
2
Solved
I am working with Typescript and firebase and I have a small abstraction layer with this function to search for a unique document base on its field name and its value.
where<K extends keyof (T ...
Philcox asked 16/7, 2021 at 15:20
3
Solved
I've been trying to create a type that consists of the keys of type T whose values are strings. In pseudocode it would be keyof T where T[P] is a string.
The only way I can think of doing this is ...
Ijssel asked 4/2, 2019 at 16:46
1
Solved
I'd like to be distinguish the following function types in conditional type checks:
type SyncFn = () => void;
type AsyncFn = (data: number) => Promise<void>;
type SyncFnWithArg = (data:...
Sandisandidge asked 1/11, 2022 at 6:44
3
Solved
I would like to declare a type-enforced array of items and be able to derive a union type from it. This pattern works if you do not explicitly give a type to the items in the array. I am not sure h...
Sierra asked 2/3, 2020 at 20:37
1
So from:
export interface Category{
val: string;
icon: string
}
const categoryArray: Category[] = [
{
val: 'business',
icon: 'store'
},
{
val: 'media',
icon: 'video'
},
{
val: 'people',...
Wes asked 13/1, 2022 at 13:56
2
Solved
Is there a way to make the following type check?
function getNumberFromObject<T>(obj: T, key: keyof T): number {
return obj[key] // ERROR: obj[key] might not be a number
}
I want to speci...
Lujan asked 5/9, 2018 at 15:8
2
Solved
I have a simple Typescript function like this:
function getProperty<T, K extends keyof T>(obj: T, key: K): number {
return obj[key]; // This line is not compiling.
// Typescript will yell:...
Abutter asked 8/3, 2019 at 9:36
5
Solved
Explain to me what keyof typeof means in TypeScript
Example:
enum ColorsEnum {
white = '#ffffff',
black = '#000000',
}
type Colors = keyof typeof ColorsEnum;
The last row is equivalent to:
type...
Corrective asked 27/3, 2019 at 12:36
2
Let's say we have TypeScript code that looks like:
type User = {
id: number,
name: string,
}
let user1: User = {id: 123, name: "Hello"};
let user2: User = {id: 456, name: "World"};
let keys: (...
Byrn asked 1/11, 2019 at 8:46
1
Solved
Apologies, I'm sure this has been answered somewhere, but I'm not sure what to google. Please do edit my question if the terms in the title are wrong.
I have something like this:
type RowData = Rec...
Lamont asked 9/11, 2020 at 1:20
1
Solved
In TypeScript, some types are defined using extends keyof or in keyof. I have tried to understand what they mean, but so far I didn't succeed.
What I got is that keyof alone returns a union type wh...
Presidio asked 3/8, 2019 at 10:46
1
Solved
I need to use some subset of class properties names as values in a map to use inside of the class. In following example I've replaced map by array. The problem is that if property is marked private...
Goerke asked 16/7, 2019 at 22:5
1
Solved
I want to have a method that accepts an array of objects and an array of some of the objects keys. The method will return an array of arrays of object values but only of the selected keys.
data:
...
Raul asked 28/5, 2019 at 12:42
1
Solved
I wondering how to correctly infer 2th and 3th template of my function
suppose a simple interface
interface ISome {
a: string;
b?: {
c: string;
};
}
The following works:
function pathBuilder&l...
Arcadia asked 18/4, 2019 at 23:18
1
Solved
In one file I have something like this:
export const _all = {
a: '',
b: '',
c: '',
d: '',
e: '',
f: '',
}
type AllKeysType = typeof _all;
export type AllKey = keyof AllKeysType;
In another...
Capillaceous asked 7/3, 2019 at 14:30
2
Solved
I would like to declare a type that requires all the keys of a given type T to be included in an array, e.g.:
checkKeys<T>(arr: Array<keyof T>): void {
// do something
}
interface My...
Beastly asked 26/8, 2018 at 17:43
2
I want to define an Array type that must contain a chain of nested property names of a given type.
Let's say I have a type:
type Foo = {
outer: {
inner: any;
}
}
Now I want to define an Ar...
Mill asked 27/2, 2018 at 9:24
2
I try to write a generic function which assembles update data for database updates.
Passed arguments:
record to be updated
property key
a new array item
Even though I restrict the key's type u...
Leftist asked 22/7, 2017 at 19:47
1
© 2022 - 2025 — McMap. All rights reserved.