Category "typescript-generics"

Condtional type is not working properly as return type

I'm trying to make a settings system for my desktop app. having a config that includes several settings like add_while_paused, min_chars, and startup_settings (

Return subset of union depending on class in generic function

This is very much like my other question, but this time using classes instead of plain objects. class Error1 extends Error { constructor(message: string, pu

Typescript generic type constraint with conditional type, avoid circular type constraint

Suppose the following example class class X<T> { constructor(readonly t: Extract<'a' | 'b', T>) {} } The point is that the type function Extract

MethodOf type cannot be used to call the method

I would like to dynamically generate some tests, for that I have to call a method with the method name to be called and then all the test setup is done and the

Why am I getting "Type instantiation is excessively deep and possibly infinite"?

Playground link I have these general definitions: type Module<P extends Payloads, C extends Children> = { payloads: P; children: C; }; type Child

TypeScript: Can anyone difference between unknown and any? [duplicate]

I have a type: type Action<I extends unknown[], O> = (...args: I) => Promise<O>; and some test cases: // should be [boolean, s

Generics in typescript object indexers

I've been trying to make a type whose indexer has a return type which is based upon a string literal for the key. export enum Units { Enum = 'enum', Str

Tuple argument type inference in Typescript

Is it possible to achieve the folowing in Typescript: I want the type of state.initial to be deduced as a tuple based on the input to the takeConfig. It would a

Generic type parameter extending type "any" is not of type "any" anymore

I was not able to find an answer to the question why function thisDoesNotWork causes an error. In contrast, function thisWorks transpiles the line anyArgs.whate

TypeScript: Is it possible to safely access nested properties of an object given an array of keys? Can this be done in a type safe and composable way?

I'd like to write a function which gets a value from an object given an array of property keys. It would look something like this: function getValue<O, K ext

How to narrow down the union type of a class generic?

This class holds either a single Item or an array of items Item[], determined by a setting at runtime (this.config.isMultiple). How can I narrow down the class'

TypeScript failes to infer return type of constrained generic function in some cases, why is this happening?

I have this code, which works just fine in a simple case, meaning the "permissionList" const is type inferred, VSCode can suggest me the properties, such as "pe

Typescript: Type 'Promise<void>' is not assignable to type 'void | Destructor'

The editor is showing Type 'Promise' is not assignable to type 'void | Destructor'. for the checkUserLoggedIn() call in useEffect. I can get rid of it by doin

ndarray type annotation typescript

I created a function that creates an array from a given shape with typescript. But how do I specify the return type of that function as it will vary depending o

Create child class object from parent class static method typescript

I have two classes User and Model User class extends Base class Model. class User extends Model {} Model class has a static method createFrom static createFrom

TypeScript create type from generic argument

Say I create an instance of my class like: var x = new SomeClass<SomeModel>() Where the definitions are: interface SomeModel{ someString: string so

Typescript generic object in array only match the specified Records

Demo: https://tsplay.dev/Nnavaw So I have an array with the following definition: Array<{ id?: string; text?: string; date?: Date; }>

Typescript: Evaluate type of generic function

Is there any trick to "evaluate" the type of a generic fuction? Consider the following: type Arr = <A>() => A[] type Ev<G, A> = ??? Question: I

Build a complex type from Union type

I'm trying to build a type with two layers from a flat union type Here's my code: type TextVariants = | { size: 'tiny' // available variants for t

How to use union type as a generic parameter in a record which in a function

I'm confused about the generic union type in Record. This is my code (TypeScript version: 4.6.3): const fn = <T extends number | string = string>() =&g