Typescript: deep keyof of a nested object, with related type

In order to achieve this goal we need to create permutation of all allowed paths. For example: type Structure = { user: { name: string, surname: string } } type BlackMagic<T>= T // user.name | user.surname type Result=BlackMagic<Structure> Problem becomes more interesting with arrays and empty tuples. Tuple, the array with explicit length, should be … Read more

Typescript: How can I make entries in an ES6 Map based on an object key/value type

Here’s the closest I can imagine getting, although I still don’t understand why we don’t just use plain objects to begin with: type ObjectToEntries<O extends object> = { [K in keyof O]: [K, O[K]] }[keyof O] interface ObjectMap<O extends object> { forEach(callbackfn: <K extends keyof O>( value: O[K], key: K, map: ObjectMap<O> ) => void, … Read more

Types for function that applys name of function and arguments

The compiler will not be able to understand that this is type safe because it generally does not reason very well about assignability for types that depend on as-yet-unspecified generic type parameters. There is an existing GitHub issue, microsoft/TypeScript#24085, that describes this situation. In fact, it is possible (but not very likely) that in your … Read more

In TypeScript, how to get the keys of an object type whose values are of a given type?

This can be done with conditional types and indexed access types, like this: type KeysMatching<T, V> = {[K in keyof T]-?: T[K] extends V ? K : never}[keyof T]; and then you pull out the keys whose properties match string like this: const key: KeysMatching<Thing, string> = ‘other’; // ERROR! // ‘”other”‘ is not assignable … Read more

How to fix TS2322: “could be instantiated with a different subtype of constraint ‘object'”?

Complementing @fetzz great answer. SHORT ANSWER TLDR; There are two common causes for this kind of error message. You are doing the first one (see below). Along with the text, I explain in rich detail what this error message wants to convey. CAUSE 1: In typescript, a concrete instance is not allowed to be assigned … Read more