Returning an Array in flatMap seems to produce the wrong TypeScript type

I’m assuming this is user error on my part, but when I write the code below, I’m expecting the return type to be User[] but TS seems to think it’s User[][] even though I’m calling flatMap(). I’m not sure if my generics are messing something up, but it seems like TS doesn’t grok that the… Read More Returning an Array in flatMap seems to produce the wrong TypeScript type

Typescript: How to wrap interface properties?

Suppose I have the following definitions: interface ISomeInterface { propertyA: string, propertyB: number, propertyC: boolean, propertyD: ISomeOtherInterface // bonus points for this one! } class MyGeneric<T> {} class MyGenericContainer<T> {} now I want to "wrap" all properties, so that I recieve something like that: interface ISomeWrappedInterface { propertyA: MyGeneric<string>, propertyB: MyGeneric<number>, propertyC: MyGeneric<boolean>, propertyD: MyGenericContainer<ISomeOtherInterface>… Read More Typescript: How to wrap interface properties?

How to infer type of interface property by passing its property as argument?

here is my example: export const getField = <T extends Record<string, any>, K extends keyof T = keyof T>(fieldName: K) => { const record = injectRecord<T>(); // sideEffect return { field: record[fieldName] }; }; // in real life here will be different objects const injectRecord = <T>() => ({ number: 123, bool: true, string: ‘asd’… Read More How to infer type of interface property by passing its property as argument?

How could 'F' be instantiated with a different subtype of constraint '(…a: Parameters<F>) => number'?

If the return value derives its rest parameter type from F, how could F be instantiated with a different subtype? function wrap<F extends (…a: Parameters<F>) => number>(f: F): F { return (…a: Parameters<F>) => f(…a) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } Type ‘(…a: Parameters<F>) => number’ is not assignable to type ‘F’. ‘(…a: Parameters<F>) => number’ is assignable to… Read More How could 'F' be instantiated with a different subtype of constraint '(…a: Parameters<F>) => number'?

Error with super structure in inheritance attempt (typescript classes)

class Character { public readonly name: string; public readonly level: number; constructor(name: string, level: number) { this.name = name; this.level = level; } walk() { return `${this.name} walking!` } showMyLevel() { return `${this.name} has the level ${this.level}` } } class Wizzard extends Character { private readonly cajado: string; constructor(cajado: string) { super(name, level); this.cajado =… Read More Error with super structure in inheritance attempt (typescript classes)

typescript: Why does my default generic type results the determination that the param could be of that type only?

I tried to use a generic type with a default value and use inference based on it to determine other function arguments, but it seems to determine that the default type is the only type allowed for that argument, is that supposed to happen? am i doing something wrong? this is the code snippet: type… Read More typescript: Why does my default generic type results the determination that the param could be of that type only?