Generic types — interfaces, classes and default type arguments¶
Level: 201 · for anyone who has used a generic type such as Promise
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: With nothing to infer from, new Stack() becomes Stack<unknown> and accepts any push; a default <T = string> makes new Queue() a Queue<string>; and the standard library's new Map() is Map<any, any>, which checks nothing.
Keywords: generic class, generic interface, default type argument
What the finished page will answer¶
- What type does
new Stack()get when nothing is passed andThas no default, and what can you then push onto it? - What does a default
<T = string>change fornew Queue(), forQueue<number>and for a bareQueueannotation? - Why is
new Map()aMap<any, any>whilenew Set()is aSet<unknown>, and how donew Map<string, number>()or an annotation fix it? - Why can a static member of
class Stack<T>not useT, and what does TS2302 say? - Why must a defaulted type parameter come after the required ones, and what does TS2706 say about
<T = string, U>?
Examples it will need¶
-
generic_types_no_argument_sh.sh— the declarations tsc emits for new Stack(), new Queue(), new Map() and new Set(): unknown, the default, any and unknown -
generic_types_defaults_tserror.ts— the TS2302 error for a static member using T and the TS2706 error for a required parameter after a defaulted one -
generic_types_stack_ts.ts— a Stack of numbers and a default Queue in use, with their contents after each push and pop
See also¶
- Type aliases and interfaces — the two ways to name a shape, both of which take parameters
MapandSet— the collections whose empty constructors are typed differentlyanyis contagious — where the any from new Map() goes next- Static members and static blocks — static members belong to the class, which has no single T
- Rust: Generic enums ↗ — generic enums in Rust, where Option is ordinary library code