typescript-typings

redeclare incorrect typsescript type in 3rd party library

孤人 提交于 2021-02-10 05:20:08
问题 I'm using winston 3.0 with the @types/winston types. These types are not yet fully compatible, and I've come across an error in the types which I don't know how to correct. Here's my code. logger.ts export function middleware(): express.Handler { const transport = new winston.transports.Console({ json: true, colorize: true, stringify: getStringify() }); const loggerOptions: expressWinston.LoggerOptionsWithTransports = { transports: [transport], meta: true, msg: "HTTP {{req.method}} {{req.url}

Typescript Union To Intersection returns values as never

核能气质少年 提交于 2021-02-08 05:25:11
问题 My question is with reference to this post Transform union type to intersection type Whenever i convert a union To Intersection i loose the union type, here is some code i wrote to get around the issue type SomeUnion = 'A' | 'B'; type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never type UnionToInterSectionWoNever<T> = { [K in keyof UnionToIntersection<T>]: UnionToIntersection<T>[K] extends never ? T[K] : UnionToIntersection<T>[K] };

TypeScript: enforcing a single dynamic key on an object

徘徊边缘 提交于 2021-02-08 05:23:31
问题 Is there a way to write an interface for an object that has a single dynamically-named key? I'm able to write an interface that accepts any number of dynamically-named keys, but I'd like to restrict it to just one. Let's start with some basics, and work our way up to my problem. In the following interface, the object can only have a single key, and it is named "id": interface Test { id: string } This is good in that objects with this interface can only have one property, id . But I need the

TypeScript: enforcing a single dynamic key on an object

久未见 提交于 2021-02-08 05:23:27
问题 Is there a way to write an interface for an object that has a single dynamically-named key? I'm able to write an interface that accepts any number of dynamically-named keys, but I'd like to restrict it to just one. Let's start with some basics, and work our way up to my problem. In the following interface, the object can only have a single key, and it is named "id": interface Test { id: string } This is good in that objects with this interface can only have one property, id . But I need the

Typescript / Type Safe Curried Functions

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 20:55:19
问题 How to safely type curried functions in typescript? With particular regard to the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; } const prop: Prop = (key, object) => object[key]; const valid1 = prop('foo', { foo: 'hello' }); // string const valid = prop('foo')({ foo: 'hello' }); // string // `never`, since `baz` does not exist in { foo: string } const invalid = prop('baz')({ foo: 'hello' }); // never 回答1:

Typescript / Type Safe Curried Functions

耗尽温柔 提交于 2021-02-07 20:49:34
问题 How to safely type curried functions in typescript? With particular regard to the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; } const prop: Prop = (key, object) => object[key]; const valid1 = prop('foo', { foo: 'hello' }); // string const valid = prop('foo')({ foo: 'hello' }); // string // `never`, since `baz` does not exist in { foo: string } const invalid = prop('baz')({ foo: 'hello' }); // never 回答1:

Typescript / Type Safe Curried Functions

谁都会走 提交于 2021-02-07 20:48:45
问题 How to safely type curried functions in typescript? With particular regard to the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; } const prop: Prop = (key, object) => object[key]; const valid1 = prop('foo', { foo: 'hello' }); // string const valid = prop('foo')({ foo: 'hello' }); // string // `never`, since `baz` does not exist in { foo: string } const invalid = prop('baz')({ foo: 'hello' }); // never 回答1:

Typescript / Type Safe Curried Functions

六眼飞鱼酱① 提交于 2021-02-07 20:48:29
问题 How to safely type curried functions in typescript? With particular regard to the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; } const prop: Prop = (key, object) => object[key]; const valid1 = prop('foo', { foo: 'hello' }); // string const valid = prop('foo')({ foo: 'hello' }); // string // `never`, since `baz` does not exist in { foo: string } const invalid = prop('baz')({ foo: 'hello' }); // never 回答1:

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, ..