TypeScript · Lesson 57 of 120
Default Type Parameters
Sensible defaults.
A type parameter can have a default, just like a function parameter. If nobody says what T is, the default is used.

- Defaults
- Like function parameters
- Used when not given
A default type
Event of T defaults to a record of strings. The first event uses the default. The second one fills in its own payload type. Run it.
app.tstypescript
interface AppEvent<T = Record<string, string>> {
name: string;
payload: T;
}
const login: AppEvent = { name: "login", payload: { user: "sam" } };
const sale: AppEvent<{ total: number }> = { name: "sale", payload: { total: 65 } };
console.log(login.payload.user, "|", sale.payload.total);Output
sam | 65
Summary
Default type parameters fill in T when no type is given. Next, practice with a typed store.
Key takeaways- <T = Default>
- Optional
- Override when needed
Next up: Practice: A Typed Store — generics together