my-awesome-typescript-project / src/conditional-types
Conditional types.
type conditional<T> = conditional<T>;Defined in: src/conditional-types.ts:41
Create a conditional generic type with the extends keyword
| Type Parameter |
|---|
T |
- Conditionals work with any type (primitive, custom, indexed access, etc).
type constrained<T> = constrained<T>;Defined in: src/conditional-types.ts:48
Constrain a generic type using a condition
| Type Parameter |
|---|
T |
type typeNarrowing<T> = typeNarrowing<T>;Defined in: src/conditional-types.ts:57
Type narrowing (type equivalent of code narrowing)
| Type Parameter |
|---|
T extends objectShape |
- Complex type narrowing can be achieved by
nestingconditional types.
const possible: typeNarrowing<string>;Defined in: src/conditional-types.ts:77
Type narrowing example 1
const alsoPossible: typeNarrowing<objectShape>;Defined in: src/conditional-types.ts:83
Type narrowing example 2
type echoingType<T> = echoingType<T>;Defined in: src/conditional-types.ts:93
infer keyword.
| Type Parameter |
|---|
T |
infercomplements conditionals and cannot be used outside an extends clause.inferis used within conditionals to declare a type variable within the constraint.
type echoedType = number;Defined in: src/conditional-types.ts:102
Simplest possible use of infer
inferallows dynamic capture of types in thee extends clause and can be thought of as "unwrapping a type".
type layerOne<T> = layerOne<T>;Defined in: src/conditional-types.ts:109
"Inner" type wrapper
| Type Parameter |
|---|
T |
type layerTwo<T> = layerTwo<T>;Defined in: src/conditional-types.ts:116
"Outer" type wrapper
| Type Parameter |
|---|
T |
type unwrap<T> = unwrap<T>;Defined in: src/conditional-types.ts:131
Unwrapping of nested types.
| Type Parameter |
|---|
T |
- Unwrap nested types using
inferand conditionals.
type impossibleType<T> = impossibleType<T>;Defined in: src/conditional-types.ts:147
Using generic type with inferred type.
| Type Parameter |
|---|
T |
- Returned types for constrained (shapeIfString) do not match supported types for typeNarrowing (objectShape | string).
- As a result, any type created from this generic evaluates to never.
type NonDistributive<T> = NonDistributive<T>;Defined in: src/conditional-types.ts:170
Use an union type with a generic type
| Type Parameter |
|---|
T |
type Distributive<T> = Distributive<T>;Defined in: src/conditional-types.ts:177
Distribute a generic type over a union of types
| Type Parameter |
|---|
T |
const one: NonDistributive<string | symbol>;Defined in: src/conditional-types.ts:183
Non- distributive : each array element is a union of types