generic constraint
A rule on a type parameter that limits which types may fill it, such as requiring a class, a particular interface or a certain shape.
In more detail
Without a constraint a type parameter could be anything, so generic code can do almost nothing with it. A constraint lets the code rely on what the constraint promises, and callers passing an unsuitable type get a compile error. C# writes constraints with where, and TypeScript writes them with extends.
Examples
C#: where TKey : notnullTypeScript: T extends { id: string }Related terms
- generics — Types that take other types as parameters, so one definition works for many kinds of value.
- extends — A keyword with three jobs in TypeScript: a class inheriting from another, a limit on a generic type parameter, and a tsconfig.json inheriting a base file.
- keyof — The TypeScript type operator that produces a union of an object type's property names, such as "id" | "table" | "rowCount".
Taught in
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices
- Generic Interfaces, Classes And Type Aliases — Production TypeScript
- Generic Constraints: extends, keyof And Indexed Access — Production TypeScript