The more you work with generic, the more you may find that for a particular case in your system you are using always the same type. It could almost not be generic but be of a specific type. In that case, it is interesting to use a default type for your generic. A default generic type allows avoids having to specify a type. A default generic is also known as an optional type.
TypeScript uses the type specified in the generic signature after the equals sign:
interface BaseType<T = string> { id: T; } let entity1: BaseType; let entity2: BaseType<string>; let entity3: BaseType<number>;
Three variables are declared. The first and second ones are exactly the same: they expect an object with an id
of a string
type. The last is a number. The reason the first and second are exactly the same is that the first declaration relies on the default type. The default type is specified in the generic definition of the interface after the name of the type, T
. The use of the equals...