Skip to content

Latest commit

 

History

History
422 lines (255 loc) · 10.1 KB

File metadata and controls

422 lines (255 loc) · 10.1 KB

my-awesome-typescript-project


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

src/generic-types

Generic types.

Table of contents

1. Generic shapes

genericInterface

Defined in: src/generic-types.ts:28

Generic interface

Remarks

  • Defines public contracts (no need to define constructor).
  • Read the notes for best practices on accessors.

Type Parameters

Type Parameter
T

Methods

setOne()
setOne(one): void;

Defined in: src/generic-types.ts:29

Parameters
Parameter Type
one T
Returns

void

getOne()
getOne(): T;

Defined in: src/generic-types.ts:30

Returns

T

2. Generic implementations

genericClass

Defined in: src/generic-types.ts:42

Generic class

Remarks

  • Can be made more adaptable if T is an object shape instead of a primitive type.
  • Use indexed types to access object shape inner types and type private properties.
  • Class expressions are a pain when made generic because of tsconfig's isolatedDeclarations, avoid.

Type Parameters

Type Parameter
T

Implements

Constructors

Constructor
new genericClass<T>(one): genericClass<T>;

Defined in: src/generic-types.ts:44

Parameters
Parameter Type
one T
Returns

genericClass<T>

Methods

setOne()
setOne(one): void;

Defined in: src/generic-types.ts:45

Parameters
Parameter Type
one T
Returns

void

Implementation of

genericInterface.setOne

getOne()
getOne(): T;

Defined in: src/generic-types.ts:46

Returns

T

Implementation of

genericInterface.getOne


e

const e: InstanceType<typeof genericClass>;

Defined in: src/generic-types.ts:56

Generic class usage

Remarks

  • Removing the type annotation on the constructor call makes the compiler infer the type from the argument, avoid.
  • Utility types are useful for annotating exports of generic-based classes instances.

3. Generic functions

genericFn()

type genericFn<T, U> = (a, b) => [U, T];

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

Generic function type expression

Type Parameters

Type Parameter
T
U

Parameters

Parameter Type
a T
b U

Returns

[U, T]

Remarks

  • Declare a (simple here) function signature over 2 generic types.
  • Returned value must be a tuple of types (subtype of the array).
  • Best practices :
    1. Always use as few type parameters as possible.
    2. When possible, use the type parameter itself rather than constraining it.
    3. If a type parameter only appears in one location, strongly reconsider if you actually need it.
  • The same result can be achieved with interfaces, however types are preferred.
  • Note : it is not possible to create generic enums and namespaces.

generic

const generic: genericFn<string, number>;

Defined in: src/generic-types.ts:87

Function implementation using a generic

Remarks

  • Always separate signature declaration and function implementation (for clarity).
  • Typescript can also infer types from generic functions arguments.
  • However, it is better to always provide functions implementations with specific types as seen below.

4. Constrained generics

constrainedFn()

type constrainedFn<T> = (o) => string;

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

Constrained generic types

Type Parameters

Type Parameter
T extends objectShape

Parameters

Parameter Type
o T

Returns

string

Remarks

  • Equivalent of type constrainedFn<T> = T extends objectShape ? (o: T)=> string : never;, which one is better is debatable.

constrained

const constrained: constrainedFn<
  | objectShape
| extendedShape>;

Defined in: src/generic-types.ts:125

Constrained function implementation and typing.

Remarks


constrainedGenericClass

Defined in: src/generic-types.ts:144

Constrained generic class.

Remarks

  • Extending a generic interface to isolate declaration makes no point in this case.
  • Note : _ static class members cannot use the type parameters_.
  • See also this guide on how to set defaults for type parameters.
  • This example also illustrates the use of indexed access types.

Type Parameters

Type Parameter
T extends

Constructors

Constructor
new constrainedGenericClass<T>(z): constrainedGenericClass<T>;

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

Parameters
Parameter Type
z T
Returns

constrainedGenericClass<T>

Properties

Property Type Defined in
six T src/generic-types.ts:145
seven T["three"] src/generic-types.ts:146

Methods

eight()
eight(): number;

Defined in: src/generic-types.ts:152

Returns

number


callableProps

type callableProps = "three" | "four";

Defined in: src/generic-types.ts:163

Union of callable object properties.

Remarks

  • keyof doesn't work here, keys must be declared using a dedicated union type.

makeCallable()

type makeCallable<T, R> = (this, ...s) => ReturnType<T[R]>;

Defined in: src/generic-types.ts:171

Generic callable object type.

Type Parameters

Type Parameter
T extends objectShape
R extends callableProps

Parameters

Parameter Type
this T
...s Parameters<T[R]>

Returns

ReturnType<T[R]>

Remarks

  • From there, any object that matches the generic type can be made callable.

callableLiteral

type callableLiteral = objectShape & makeCallable<objectShape, "three">;

Defined in: src/generic-types.ts:181

Type an existing object shape as callable.

Remarks

  • Use the generic to bind the object call signature to on if its methods.

callThisLiteral

const callThisLiteral: callableLiteral;

Defined in: src/generic-types.ts:189

Implement a typed callable object literal.

Remarks

  • Declare literal as a function then add missing keys, compiler OK.