Questions tagged [conditional-types]

Questions about [Conditional Types](https://www.typescriptlang.org/docs/handbook/advanced-types.html]) in Typescript.

Conditional types are a Typescript feature to allow a generic entity to have some non-uniform variation in its type depending on its generic arguments.

170 questions
41
votes
2 answers

Why is the infer keyword needed in Typescript?

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 extends (...args: any[]) => infer R ? R : any; I don't understand why this is needed. Why can't it…
CodyBugstein
  • 17,496
  • 50
  • 159
  • 312
25
votes
1 answer

Typescript: Type of a property dependent on another property within the same object

I have a TypeScript interface with two properties (type:string and args:object). The args may have different properties depending on the type. What type definition to I need to apply to args so the compiler/autocomplete will know which properties…
smigfu
  • 355
  • 3
  • 8
25
votes
2 answers

How do I type an object with known and unknown keys in TypeScript

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 [U: string]: string } const comboObject:…
Jacob Gillespie
  • 3,131
  • 2
  • 18
  • 29
18
votes
1 answer

TypeScript conditional types - filter out readonly properties / pick only required properties

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... interface I1 { readonly n: number s: string } I…
DanielM
  • 913
  • 2
  • 13
  • 25
15
votes
1 answer

Optional parameters based on conditional types

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 = ( first: T, second: T extends string ? boolean :…
torkel
  • 1,102
  • 1
  • 5
  • 16
14
votes
2 answers

Is there a nice way to implement a conditional type with default fail case?

For implementing a conditional type I highly enjoy std::conditional_t as it keeps the code short and very readable: template using bit_type = std::conditional_t
12
votes
3 answers

TypeScript conditional types: Extract component props type from react component

Using TypeScript 2.8 new conditional generic type feature, is it possible to extract the TProps of a React.ComponentType component? I want a type that can either work on the ComponentType or the TProps itself, so you can - as a developer -…
Dynalon
  • 5,606
  • 9
  • 46
  • 72
11
votes
1 answer

Conditional type based on value of variable in a class (TypeScript)

I'm creating a generic class named Loadable, with two fields. One is named state, and contains the current state of the loadable resource. The second is named data, and contains the value of the loadable resource. state is defined as "loading" |…
hdt80
  • 349
  • 3
  • 12
9
votes
2 answers

Trying to understand the limits of 'T extends infer U'

I've understood that something like: type GenericExample = T extends (infer U) ? U : 'bar'; is equal to: type GenericExample = T extends T ? T : 'bar'; But when stuff becomes more elaborate, TypeScript complains: type Types = 'text' | 'date'…
9
votes
1 answer

Exclude object keys by their value type in TypeScript

I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric, where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number…
prmph
  • 4,827
  • 7
  • 31
  • 35
8
votes
4 answers

Mixing union types, generics and conditional types causes unexpected "Type is not assignable to type" error

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 problem in action at this playground link. Consider the…
spender
  • 106,080
  • 28
  • 202
  • 324
7
votes
1 answer

Conditional type doesn't recognize that all inputs result in same conditional result

This example doesn't typecheck: type Subset1 = "one" | "two"; type Subset2 = "three" | "four"; type All = Subset1 | Subset2; type Other = { "one": number, "two": string, "three": boolean, "four": object, }; type Extra = V…
loganfsmyth
  • 135,356
  • 25
  • 296
  • 231
7
votes
3 answers

How to conditionally detect the `any` type in TypeScript?

I would like an utility that I can use as IsStrictlyAny 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: type IsStrictlyAny = any extends T ? true :…
Pedro A
  • 2,582
  • 2
  • 19
  • 46
7
votes
1 answer

How to make object property optional based on other type in TypeScript?

I think the best way to explain my scenario is with code: interface IPluginSpec { name: string; state?: any; } interface IPluginOpts { name: PluginSpec['name']; // How to require opts.initialState ONLY when…
treznik
  • 7,455
  • 12
  • 41
  • 55
7
votes
1 answer

Typescript: Return type of function based on input value (enum)

I'm storing some settings into local storage and I would like to type the responses when I get (and ideally also insert) values from/to the storage. From what I've seen, the best way seems to be to use function overloading. So this is what I have…
1
2 3
11 12