typeover
curriculum

Curriculum Interfaces & generics Generics exercise 3 · mcq

Generics

TypeScript constrains a generic with a union of literal types or a base interface. Go uses a TYPE SET constraint — a union of TYPES, separated by |, written inline as the constraint expression OR named as an interface. Pick the Go form that constrains T to either int OR float64.

TypeScript reference
Pick the idiomatic Go translation

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.