Generic Types

One of the most powerful aspects of TypeScript's type system is the ability to express types in terms of other types. This section builds on the fundamentals of types to include a range of powerful templating options you have at your disposal when building TypeScript codebases.

Generics

A generic function accepts parameter types alongside its regular parameters.

The easiest generic function to create is the identity function, which simply returns its first argument.

We can modify this function to use the any type, which indicates that it works correctly for any possible input (it does!)

Notice that the type of x in the program above is any, even though it should be fairly obvious that the result will be of the same type string. To allow TypeScript inference to "pass through" the function, we can define a type variable after the function name to capture information about the first parameter's type.

Notice that x is now correctly typed as a string, and will receive the same type that TypeScript inferred for the first argument 'hello'.

There may be situations where you need to explicitly tell TypeScript what the first parameter's type is.

Just as with function parameters, type parameters can be assigned a default value.

Enums

An enum is a collection of named constants.

Loading TypeScript...

Type flattening

The process of "type flattening" is technically called indexed access on a mapped type.

The indexed access type [keyof T] accesses the union of all value types for the mapped object type T. The keyof operator is widely used for accessing both the key types and value types of an object type.

The typeof operator

The typeof operator is commonly used in JavaScript to determine a value's data type when a program is running (i.e. at runtime). TypeScript also allows you to use typeof to retrieve the type of a value at compile time.

This is especially useful when we want to retrieve the return type of a function.

Notice that we cannot use the function name f as a type, since f is a value.

If you examine the JavaScript that is generated from this program, the name compileType is nowhere to be found - it is a type construct that only exists for the sake of the compiler. Just as we can extract some basic type information for f at runtime with typeof f, we can use the same expression typeof f to extract detailed type information from the TypeScript compiler.

Limitations

TypeScript intentionally limits the expressions that you can use typeof on.

Specifically, you can only use typeof on variable names or their properties.

Was this page helpful?