conditional-types Questions

4

Solved

Is it possible to make a function have either mandatory or optional parameters based on conditional types in TypeScript? This is what I've got so far: const foo = <T extends string | number&gt...

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

Solved

Using the new conditional types in TypeScript (or maybe another technique), is there a way to pick only certain properties from an interface based on their modifiers? For example, having... interf...
Conjugate asked 30/3, 2018 at 17:55

4

Solved

I am looking for a way to create TypeScript types for the following object that has two known keys and one unknown key that has a known type: interface ComboObject { known: boolean field: number...
Martynne asked 22/4, 2018 at 18:33

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

3

Is it possible to infer the constructor type of a class in TypeScript? I tried this but it seems not to work: type Constructor<K> = K extends { new: infer T } ? T : any;
Gawky asked 26/4, 2019 at 11:16

1

Solved

I'd like to be distinguish the following function types in conditional type checks: type SyncFn = () => void; type AsyncFn = (data: number) => Promise<void>; type SyncFnWithArg = (data:...
Sandisandidge asked 1/11, 2022 at 6:44

2

Solved

Given an object type that has optional properties, such as: interface Person { name: string; age: number; friends?: Person[]; jobName?: string; } … I'd like to remove all of its optional prope...
Wiles asked 5/7, 2021 at 13:56

2

Solved

I want to type objects within an array, so that one property of the object (here foo) implicitly defines the type for another property (bar) for each object seperately. Is that possible? type Foo&l...

3

Solved

The following compiles on Visual Studio: template<typename ArgType, typename ReturnType> struct Test { using FunctionPointerType = std::conditional_t< std::is_same_v<ArgType, void>...
Deft asked 25/5, 2022 at 16:20

2

Solved

Background As part of a data analysis library I'm working on, I'm creating a set of functions that read certain types of values out of strings. The idea is essentially to define the structure of a ...
Bowser asked 2/1, 2022 at 1:57

4

Solved

Why did the Typescript folks create the infer keyword? According to the documents, this is an example of how you would use it: type ReturnType<T> = T extends (...args: any[]) => infer R ?...
Nephridium asked 4/2, 2020 at 23:39

3

I've understood that something like: type GenericExample<T> = T extends (infer U) ? U : 'bar'; is equal to: type GenericExample<T> = T extends T ? T : 'bar'; But when stuff become...
Pleo asked 6/4, 2020 at 8:19

1

Solved

This example doesn't typecheck: type Subset1 = "one" | "two"; type Subset2 = "three" | "four"; type All = Subset1 | Subset2; type Other = { "one"...
Favour asked 18/1, 2021 at 23:45

2

Solved

I'm having a hard time understanding how to stop code from being evaluated with std::conditional_t in the false branch. #include <type_traits> using namespace std; namespace { templa...
Discobolus asked 11/12, 2020 at 20:6

2

Solved

I'm trying to write a function that takes an object and a (string) key, then operates on a property of the object. This is easy: function f<T extends any, K extends keyof T>(obj: T, key: K) {...
Attraction asked 20/10, 2020 at 16:11

1

I was looking for something like a Nullable type in TypeScript when I noticed that while there is no Nullable type (or is there?), there is a NonNullable type defined in: C:\Program Files\Mic...
Woodall asked 29/5, 2018 at 16:23

2

Solved

Typescript playground I want to extract the string properties from SOME_OBJECT and get it as a union type. Therefore, in the example below, I expect STRING_KEYS to be of type "title" | &q...
Commorancy asked 6/10, 2020 at 15:47

4

I've hit a problem with type-inference specifically when conditional-types are used within union types. There may be a shorter way to demonstrate this issue, but I could not find one... See the pro...

3

Solved

I might be asking too much of Typescript, but I was wondering if something like this is possible: interface ObjectType { type: 'this' | 'that'; } interface SomeObject { objType: ObjectType } in...
Sheltonshelty asked 20/7, 2020 at 2:26

1

Solved

I'm reading an article :"Unionize and Objectify: A Trick for Applying Conditional Types to Objects" In TypeScript our most powerful tool is conditional types. This is because they have two uniq...
Endpaper asked 29/5, 2020 at 11:24

1

Solved

The following code is trying to define the type of a function that's called with no arguments when its generic argument is undefined, but with 1 argument for any other argument type. (There may wel...
Fleecy asked 21/5, 2020 at 3:30

3

Solved

I would like an utility that I can use as IsStrictlyAny<T> and it will resolve to the type true if T is exactly any and to the false type otherwise. How can I do this? My first idea: ...
Loleta asked 5/5, 2020 at 23:23

1

Solved

I'm trying to define helper types for determining the type of nested object values, whilst also considering any optional parent keys, e.g. in structures like these (or deeper): type Foo = { a: { b...
Lyndseylyndsie asked 26/3, 2020 at 14:22

1

Solved

I need to have a generic type that excludes a generic property from a specified type when the generic parameter (of this property) is never. To achieve this I used Omit and conditional types. When ...
Crossrefer asked 21/11, 2019 at 15:1

© 2022 - 2024 — McMap. All rights reserved.