keyof

Typescript parameters - a generic array of objects and array of object's keys (partial)

浪子不回头ぞ 提交于 2021-02-08 03:47:37
问题 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: [ {"firstName": "Jane", "lastName": "Doe"}, {"firstName": "John", "lastName": "Doe"} ] fields: ["firstName"] result: [["Jane"], ["John"]] By now I have a function that provides desired outcome but I am not sure how to handle the types better. mapToCsvData: (data: { [key: string]: any }[], fields:

In TypeScript, what do “extends keyof” and “in keyof” mean?

落花浮王杯 提交于 2020-06-10 05:18:07
问题 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 which has all the names as possible values that are existent as property names on the type that you specify after keyof . type T = keyof string; T therefor is equivalent to startsWith | endsWith | trim | substring | ... . Is this correct? Now, if I try to think about what extends keyof and in keyof

List private property names of the class

瘦欲@ 提交于 2020-01-25 04:12:06
问题 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 it's not listed in keyof list. How can I specify type of keys if I need to include private names? var keys: Array<keyof A> = ["x", "y"]; // Error class A { private x = 7; public y = 8; private keys: Array<keyof A> = ["x", "y"]; // Error } There is the same error both for variable outside of the

Expected 3 type arguments but got 1 but it should infer 2 types

给你一囗甜甜゛ 提交于 2019-12-27 17:57:45
问题 I wondering how to correctly infer 2th and 3th template of my function suppose a simple interface interface ISome { a: string; b?: { c: string; }; } follow works function pathBuilder< K1 extends keyof ISome, K2 extends keyof NonNullable<ISome[K1]>>(p: K1, p2?: K2) { let res = String(p); if (p2) { res += "." + p2; } return res; } const pathTest = pathBuilder("b", "c"); // ---> "b.c" and intellisense works on parameters but I need to generalize the function to work by specify another type ( I

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

淺唱寂寞╮ 提交于 2019-12-25 00:48:58
问题 I have the following Interface definitions. interface IComponents { root: IComponent, [key: string]: IComponent, } interface IComponent { type: string, children?: Array<keyof IComponents>; } I want that the "children" properties accept only keys of defined Components. in the case of the "root.children"-property it should only accept root, button1 and button2: const list: IComponents = { root: { type: 'panel', children: ['button1', 'button2', 'button3'] }, button1: { type: 'button' }, button2:

Partial<T> only works with inline values

荒凉一梦 提交于 2019-12-12 02:10:03
问题 Why does the code below behave like it does? Is it a bug in the TypeScript compiler or missing feature? class MyType { constructor(public readonly value1: string, public readonly value2: number) { } } function myFunction(props: Partial<MyType>): void { // Do something here } myFunction({ }); // Compiles myFunction({ value1: 'string', value2: 42 }); // Compiles myFunction({ wrongValue: true }); // Compile error!! const myValue1 = {}; const myValue2 = { value1: 'string', value2: 42 }; const

Typescript: Infer type of nested keyof Properties

∥☆過路亽.° 提交于 2019-12-07 09:23:12
问题 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 Array type with 2 elements: type PropertyList<T, K1 extends keyof T, K2 extends keyof T[K1]> = [K1, K2]; I want to use it like this: let myList:PropertyList<Foo> = ["outer", "inner"] So I want the compiler to check if the 2 contained Property names are nested property names of Foo. But I can't define

Typescript: Infer type of nested keyof Properties

≯℡__Kan透↙ 提交于 2019-12-05 15:41:52
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 Array type with 2 elements: type PropertyList<T, K1 extends keyof T, K2 extends keyof T[K1]> = [K1, K2]; I want to use it like this: let myList:PropertyList<Foo> = ["outer", "inner"] So I want the compiler to check if the 2 contained Property names are nested property names of Foo. But I can't define PropertyList with only 1 generic parameter, I get this error then: TS2314: Generic type 'PropertyList' requires