typeover
curriculum

Curriculum Interfaces & generics Generics exercise 4 · fill-word

Generics

Type the constraint that allows ANY type as the type parameter. This is the constraint you reach for when the function body doesn't need to know anything about T — e.g. an identity function or a generic container.

TypeScript reference
Fill the blanks →

About this theme

Mostly familiar from TS. func Foo[T any](x T) T { ... }. The constraint syntax is where Go adds something new: an interface used as a constraint can list types or use ~T for "any type whose underlying type is T". The comparable constraint is built-in. Method sets in constraints are powerful and worth slowing down on.