Skip to content

Latest commit

 

History

History
269 lines (161 loc) · 5.63 KB

File metadata and controls

269 lines (161 loc) · 5.63 KB

my-awesome-typescript-project


my-awesome-typescript-project / src/conditional-types

src/conditional-types

Conditional types.

Table of contents

1. Constrain generics using conditions

conditional

type conditional<T> = conditional<T>;

Defined in: src/conditional-types.ts:41

Create a conditional generic type with the extends keyword

Type Parameters

Type Parameter
T

Remarks

  • Conditionals work with any type (primitive, custom, indexed access, etc).

constrained

type constrained<T> = constrained<T>;

Defined in: src/conditional-types.ts:48

Constrain a generic type using a condition

Type Parameters

Type Parameter
T

typeNarrowing

type typeNarrowing<T> = typeNarrowing<T>;

Defined in: src/conditional-types.ts:57

Type narrowing (type equivalent of code narrowing)

Type Parameters

Type Parameter
T extends objectShape

Remarks

  • Complex type narrowing can be achieved by nesting conditional types.

possible

const possible: typeNarrowing<string>;

Defined in: src/conditional-types.ts:77

Type narrowing example 1


alsoPossible

const alsoPossible: typeNarrowing<objectShape>;

Defined in: src/conditional-types.ts:83

Type narrowing example 2

2. The infer keyword

echoingType

type echoingType<T> = echoingType<T>;

Defined in: src/conditional-types.ts:93

infer keyword.

Type Parameters

Type Parameter
T

Remarks

  • infer complements conditionals and cannot be used outside an extends clause.
  • infer is used within conditionals to declare a type variable within the constraint.

echoedType

type echoedType = number;

Defined in: src/conditional-types.ts:102

Simplest possible use of infer

Remarks

  • infer allows dynamic capture of types in thee extends clause and can be thought of as "unwrapping a type".

layerOne

type layerOne<T> = layerOne<T>;

Defined in: src/conditional-types.ts:109

"Inner" type wrapper

Type Parameters

Type Parameter
T

layerTwo

type layerTwo<T> = layerTwo<T>;

Defined in: src/conditional-types.ts:116

"Outer" type wrapper

Type Parameters

Type Parameter
T

unwrap

type unwrap<T> = unwrap<T>;

Defined in: src/conditional-types.ts:131

Unwrapping of nested types.

Type Parameters

Type Parameter
T

Remarks

  • Unwrap nested types using infer and conditionals.

impossibleType

type impossibleType<T> = impossibleType<T>;

Defined in: src/conditional-types.ts:147

Using generic type with inferred type.

Type Parameters

Type Parameter
T

Remarks

  • 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.

3. Distributed types

NonDistributive

type NonDistributive<T> = NonDistributive<T>;

Defined in: src/conditional-types.ts:170

Use an union type with a generic type

Type Parameters

Type Parameter
T

Distributive

type Distributive<T> = Distributive<T>;

Defined in: src/conditional-types.ts:177

Distribute a generic type over a union of types

Type Parameters

Type Parameter
T

one

const one: NonDistributive<string | symbol>;

Defined in: src/conditional-types.ts:183

Non- distributive : each array element is a union of types