typescript-generics

Using generic type parameter as parameter

眉间皱痕 提交于 2021-02-20 19:10:58
问题 I'm trying to create a generic function which calls another function with an any type parameter. This is what I tried: static GetInstance<T>(): T { return <T>injector.get(T); // get(param: any): any } The problem is this doesn't compile. I'm getting Cannot find name 'T' error. I tried get(typeof T) but typeof T is string "function" . What can I do? For clarification: get() method accept types. For example you can use it like this: import { MyService } from '..' constructor(){ let val = this

Using generic type parameter as parameter

家住魔仙堡 提交于 2021-02-20 19:10:35
问题 I'm trying to create a generic function which calls another function with an any type parameter. This is what I tried: static GetInstance<T>(): T { return <T>injector.get(T); // get(param: any): any } The problem is this doesn't compile. I'm getting Cannot find name 'T' error. I tried get(typeof T) but typeof T is string "function" . What can I do? For clarification: get() method accept types. For example you can use it like this: import { MyService } from '..' constructor(){ let val = this

Using generic type parameter as parameter

南楼画角 提交于 2021-02-20 19:08:20
问题 I'm trying to create a generic function which calls another function with an any type parameter. This is what I tried: static GetInstance<T>(): T { return <T>injector.get(T); // get(param: any): any } The problem is this doesn't compile. I'm getting Cannot find name 'T' error. I tried get(typeof T) but typeof T is string "function" . What can I do? For clarification: get() method accept types. For example you can use it like this: import { MyService } from '..' constructor(){ let val = this

Typescript > Generics > Union Constraint

十年热恋 提交于 2021-02-08 10:30:16
问题 Why does the typescript compiler throw the following error: Operator '+' cannot be applied to types 'T' and 'T'. , when compiling: export const addNumbersOrCombineStrings = <T extends string | number>( param1: T, param2: T ): T => param1 + param2 ? 回答1: The relevant issue in GitHub is Microsoft/TypeScript#12410. Your particular question is addressed in a comment by Ryan Cavanaugh: T + T where T extends string | number is still disallowed -- we don't feel this is a good use case because

Typescript > Generics > Union Constraint

人走茶凉 提交于 2021-02-08 10:30:03
问题 Why does the typescript compiler throw the following error: Operator '+' cannot be applied to types 'T' and 'T'. , when compiling: export const addNumbersOrCombineStrings = <T extends string | number>( param1: T, param2: T ): T => param1 + param2 ? 回答1: The relevant issue in GitHub is Microsoft/TypeScript#12410. Your particular question is addressed in a comment by Ryan Cavanaugh: T + T where T extends string | number is still disallowed -- we don't feel this is a good use case because

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:

Assignment involving generic property of generic object fails to typecheck correctly within generic function

微笑、不失礼 提交于 2021-02-07 12:49:20
问题 I have a generic function that reads or writes the caller-chosen property of a given object. I'm using type constraints to ensure that the key passed is for a property that is assignable to or from the relevant type. Calling code appears to typecheck correctly. The usage of the object's property within the implementation does not typecheck as expected. In this example I use boolean as the expected type. I've commented the lines that are not typechecking as expected. You can also see this

Types for function that applys name of function and arguments

折月煮酒 提交于 2021-01-30 09:07:11
问题 I'm trying to type in proper way function that applys name of function and argumens for this function. After that apply it and return the result. Here the code: const sum = (a: number, b: number) => a + b const concat = (a: string, b: string, c: string) => a + b + c const funs = { sum, concat } type Keys = 'sum' | 'concat' type Args<T> = T extends (...args: infer R) => any ? R : never type Sum = Args<typeof sum> type Concat = Args<typeof concat> function apply<K extends Keys>(funKey: K, ..

Types for function that applys name of function and arguments

心不动则不痛 提交于 2021-01-30 09:04:20
问题 I'm trying to type in proper way function that applys name of function and argumens for this function. After that apply it and return the result. Here the code: const sum = (a: number, b: number) => a + b const concat = (a: string, b: string, c: string) => a + b + c const funs = { sum, concat } type Keys = 'sum' | 'concat' type Args<T> = T extends (...args: infer R) => any ? R : never type Sum = Args<typeof sum> type Concat = Args<typeof concat> function apply<K extends Keys>(funKey: K, ..

Synchronize function return type with what function actually returns in TypeScript

牧云@^-^@ 提交于 2021-01-29 07:10:53
问题 I have a function that accepts a variadic list of keys and plucks properties off an object. The particular logic of the function is that if only one key is passed in, that property is returned directly, but if two or more keys are passed in then a tuple of the respective properties is returned. So I would write something like const object = { key1: 1, key2: 2, key3: 3 }; const property = pluck('key1'); // 1 const [property1, property3] = pluck('key1', 'key3'); // [1, 3]; But while this works