liveBook · Manning

Type argument

As explained in Essential TypeScript 5, Third Edition

A generic type argument supplies a concrete type for a parameterized type's placeholder.

A type argument supplies a specific type for a generic type parameter. Write it in angle brackets when creating an object or calling a generic method. In `new DataCollection<Person>(people)`, `Person` replaces the class’s `T`: the constructor and `add` accept `Person` objects, and `getItem` returns a `Person`. The resulting object has type `DataCollection<Person>`. section 12.3.1

Restrict a JavaScript Map

In `src/todoCollection.ts`, `private itemMap = new Map<number, TodoItem>();` specifies number keys and `TodoItem` values. JavaScript’s Map can otherwise hold mixed keys and values; these arguments let the TypeScript compiler reject incompatible uses. The value type also carries through `values()` into an array of to-do items. section 2.2.6

Choose or infer arguments according to how the generic is used:

  • Separate instances can use different arguments: `DataCollection<Person>` and `DataCollection<Product>` retain their respective item types. section 12.3.2
  • You can omit arguments when constructor or method inputs let the compiler infer them. Check the inferred type—initializing with the wrong types can give an unintended result. With declaration output enabled, `dist/index.d.ts` shows inferred types. section 12.3.5
  • A static method does not use its class’s instance type argument. It can define its own: `DataCollection.reverse<City>(cities)`. section 12.3.8

Arguments must also satisfy any constraint on a type parameter. A subclass may pass its parameter to a generic superclass, restrict it further, or fix the superclass’s parameter to one type. A subclass whose superclass type is fixed this way takes no type argument of its own. section 12.5 section 12.3.6

Type argument versus function argument

In `getValue<Product, "name">(p, "name")`, the first `"name"` is a literal type argument checked against `keyof Product`. The second is the string value passed to the function at runtime. The compiler can also infer the type arguments from a call such as `getValue(p, "price")`. section 13.4.2

Libraries use the same TypeScript feature. `LowSync<schemaType>` describes the structure expected from a JSON-backed database. In an Angular data source, `http.get<Product[]>(url)` specifies the expected result type, which flows into an `Observable<Product[]>`. Specify a type that corresponds to the server’s data; the type argument is not Angular-specific behavior. section 2.5 section 18.3.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through explicit constructor arguments, inferred constructor and method arguments, and their resulting types.
Examples
Shows a numbered-key Map, person and product collections, a generic property accessor, and an Angular HTTP request.
What it assumes
Builds from JavaScript Map and object creation while defining how generic placeholders receive type arguments.
What you can do after
Apply it via TypeScript generics to specify checked collection types and expected library result types.
How it connects
Connects generic type parameters to constraints, inheritance, type inference, and index type queries.

Also explained in

Compare how 5 books explain type argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.