Skip to main content

Questions tagged [typescript-generics]

For questions related specifically to usage of generic types in TypeScript.

typescript-generics
1 vote
0 answers
8 views

TS doesn't match exact type to generic? [duplicate]

type Input = 'a' | 'b'; type ResultOf<T extends Input> = T extends 'a' ? 'A' : 'B'; function foo<T extends Input>(input: T): ResultOf<T> { return input === 'a' ? 'A' : 'B'; } ...
Viktor Molokostov's user avatar
0 votes
0 answers
13 views

Constraining a generic parameter to be a record with a single key in Typescript

I would like to constrain the a generic parameter T of a class MyClass<T> such that T is valid for any record which has only 1 key: type ValidShape = { key: any; }; type InvalidShape = { ...
sbgrl's user avatar
  • 1
1 vote
1 answer
27 views

Function input conditional object keys based on value of other keys

I would like to have a function whose input object keys are statically typed depending on the given value of some key. Here is a TypeScript playground with a failing test: Playground Link export type ...
Jason Kuhrt's user avatar
0 votes
1 answer
24 views

How do i write recursive Routes type with absolute paths?

I have react-router v6 in my app, and i want all paths in router config object to be absolute But i need to ensure that path of child route of any depth begins with its parent path, otherwise app just ...
Skywrath's user avatar
0 votes
1 answer
40 views

Infer the return type based on a parameter

I've a function that takes an action "DELETE"|"RENAME"|"MOVE"|etc... I would like the return type to be different depending on the action fired. For example, "DELETE&...
OtpExhaustv2's user avatar
0 votes
0 answers
19 views

Have an issue with types when creating a Fastify plugin to decorate mongoose models using Zod

I have created a Fastify plugin to decorate Mongoose models, adding them to the Fastify interface: declare module 'fastify' { export interface FastifyInstance { models: { [key: ...
Gihan Rangana's user avatar
2 votes
1 answer
35 views

What’s the meaning of `T extends readonly unknown[] | []` in TypeScript’s function signature of `Promise.all`?

I noticed an interesting (possibly hacking) part in TypeScript’s lib.d.ts: interface PromiseConstructor { /* ... */ /** * Creates a Promise that is resolved with an array of results when all ...
Snowflyt's user avatar
  • 369
1 vote
1 answer
31 views

How to predict `type instatiation too deep and possibly infinite` error?

I'm trying to improve my knowledge of types in typescript, so I'm doing some type challenges. One of the challenges is to make a type that capitalises a generic string, as shown below (here). type ...
Crunchy Carrots's user avatar
0 votes
0 answers
53 views

Merging class definition with generic type definition

So say there is a type or interface that defines properties of actions, for example: export interface AnimalActions { jump: { height: number } move: { speed: number, offset: number } } And there ...
Marvin3's user avatar
  • 5,961
1 vote
1 answer
86 views

Replicating the mongodb project aggregation in Typescript

My scenario: I am trying to build a typesafe wrapper around node-mongodb driver. I am having a hard time figuring out the return type for project aggregation stage. Have a look at the TS Playground ...
Abd's user avatar
  • 145
0 votes
1 answer
52 views

How do I determine if a class's name exists as a key in a mapped object type?

I have a type that I'm trying to make dynamic. I currently have a hardcoded version that looks like this: import type { Text, TextOptions, } from 'pixi.js' export type ConstructorParams<T ...
Trezy's user avatar
  • 121
2 votes
1 answer
47 views

Can I make intellisense output more readable for Typescript generic with Omit

Context: I'm exposing a third party library to users, but needed to remove two properties from all functions in the library (we set them automatically). I've got a function to do that (and also run a ...
Tom Manterfield's user avatar
0 votes
0 answers
43 views

Vue 3 Generic Components: how to use a scoped slot with a generic data type inferred from an event listener prop

Click for Playground <SideSheet> <template #activator="{ onClick, ...slotProps }"> <v-btn v-bind="slotProps" @click="(e: PointerEvent) => ...
darkbasic's user avatar
  • 313
0 votes
1 answer
23 views

How to use Typescript Generics to infer type of object property

Given this simplified example code: type People = { [key: string]: { names: (string | number)[]; }; }; const people: People = { bert: { names: ["Bert", 1], },...
Xriter's user avatar
  • 3
1 vote
0 answers
28 views

Generics Airthmetic in Typescript [duplicate]

I am a beginner in Typescript. I am trying to learn generics. type Summable = string | number; interface Calculation<Type> { number1: Type; number2: Type; operation(): Type; } class ...
Kevin Martin's user avatar

15 30 50 per page
1
2 3 4 5
314