typescript-generics Questions

3

I have a case where I'm frequently checking both the value of a boolean and another union type together. While they're logically connected and could hypothetically just check one, I need to check b...
Langston asked 3/11, 2021 at 20:5

2

Solved

In our codebase we've been using T.lean() or T.toObject() and our return types would be LeanDocument<T>. Mongoose 7 no longer exports LeanDocument, and the existing migration guide suggests u...

2

Solved

I was just wondering if there is a way of handling this with generics than using any for the following. I am still trying to wrap my head around the types and generics, just wanted to stay clear of...
Sullen asked 7/8, 2020 at 11:4

0

Click for Playground <SideSheet> <template #activator="{ onClick, ...slotProps }"> <v-btn v-bind="slotProps" @click="(e: PointerEvent) => onClick(e, {...
Hibernicism asked 10/7, 2024 at 10:47

4

Solved

Say I have an interface: interface Applicative<T> {} Now I want to define a function f that: takes a function and takes a type U extending an Applicative wrapping Any and returns a U wrappi...
Imphal asked 30/11, 2015 at 8:47

0

I am trying to make the most efficient reusable wrapper for React-Select possible using typescript and formik, but I get a typescript error that says "type 'true' is not assignable to type 'fa...

3

Solved

I am using several enums as global parameters. enum color { 'red' = 'red', 'green' = 'green', 'blue' = 'blue', }; enum coverage { 'none' = 'none', 'text' = 'text', 'background' = 'background'...
Burkhardt asked 13/9, 2021 at 9:39

4

Solved

I have a constant array of strings e.g. const emojis = ['πŸ˜„', '😊', '😐', 'πŸ˜•', '😣'] as const And I want to have a type that contains a union of the indexes of that array e.g. type emojiIndexes =...
Narcolepsy asked 24/3, 2021 at 17:18

4

Solved

I have a type like this: type City = { name: string; id: number | null; }; And want to turn it into a type like this: type City = { name: string; id?: number; }; I have seen plenty of posts e...
Keitel asked 8/5, 2022 at 21:30

4

Solved

Having this example: interface Event { title:string; description:string; fromDate: Date; toDate: Date; location: { name: string; lat: number; long: number; } } Using a type something like...

2

Solved

Hi I've a basic example where I keep getting this error and I am not sure what exactly the problem is. Basically I want to pass different types to class A at runtime. In the given example it's Orde...
Cretin asked 25/3, 2021 at 16:50

6

Solved

Playground link I have these general definitions: type Module<P extends Payloads, C extends Children> = { payloads: P; children: C; }; type Children = Record<string, any>; // some va...
Frizzy asked 31/12, 2021 at 22:19

3

Solved

I'm hitting this wall since days now and I'm not sure anymore if I'm the problem or if typescript is broken... I defined a generic class with the generic extending Record<string, string>: cla...
Osteoclasis asked 7/5, 2022 at 7:0

3

found the code first in http://www.typescriptlang.org/docs/handbook/advanced-types.html#example-1 and then found this similar code in typescript codebase: https://github.com/microsoft/TypeScript/b...
Samaniego asked 5/12, 2019 at 3:47

3

I have an API/function that is intended to be used only with generic type arguments (it enforces the shape of an argument based on a generic parameter). I want to prevent calling the API without a ...
Bise asked 2/3, 2019 at 4:50

1

I've tried to create a PojoDocument<T> generic type to guard from leaking mongoose documents out of our DALs. My goal was to make sure the returned object is not a mongoose Document, but a PO...

2

I have a function with the following parameter: const handleAccount = ( account: Partial<IAccountDocument>, ... ) => { ... } In no way can I change the interface for IAccountDocument to...
Cavan asked 19/4, 2022 at 13:54

3

Solved

I have a Select component that accepts an array of options. Each option can be an object of anything as long as it has the following attributes id and text So I typed it like this: type SelectOptio...
Hospitalization asked 31/12, 2021 at 13:3

1

Solved

I'm trying to model my data in Typescript in the following manner. Main focus is the type MessageHandler that maps the type in a message with a callback that accepts that message. I followed the ha...

3

Solved

I've been trying to create a type that consists of the keys of type T whose values are strings. In pseudocode it would be keyof T where T[P] is a string. The only way I can think of doing this is ...

3

Solved

I cannot get the type of a generic method in a class, trying to do that results in a parse error. class MyClass<T extends string> { public myMethod<U extends string>(arg: U): [T, U] { ...
Thunell asked 21/3, 2023 at 8:28

1

I am trying to write a custom useQuery hook to fetch a list of exercises. The hook should also accept an optional select function. In the case where the optional select function is provided, the ty...

4

Solved

Given the following: enum FooKeys { FOO = 'foo', BAR = 'bar', } I'd like to make an interface like this one, but instead of defining keys by hand, build it out of enum's values. interface Foo...
Before asked 13/3, 2019 at 12:41

3

How do I rewrite this without overload signatures, using conditional types instead? function foo(returnString: true): string; function foo(returnString: false): number; function foo(returnString: ...
Jany asked 19/7, 2018 at 21:40

2

Solved

I have a case where I want to "merge" types where the default type joining (i.e. T | U or T & U) doesn't achieve what I want. What I am trying to make is a deep and smart type merge that will ...
Yahairayahata asked 22/3, 2020 at 2:7

© 2022 - 2025 β€” McMap. All rights reserved.