Curriculum Errors & packaging Type assertions & type switches exercise 4 · fill-word
Type assertions & type switches
Type the keyword that turns a type assertion into a TYPE
SWITCH. Goes in parens where a single type would normally
go (v.(type) vs v.(*MyError)). Lowercase, four letters.
TypeScript reference
Fill the blanks →
About this theme
v.(T) asserts that an interface value v is actually of type T. Panics if wrong. Use the comma-ok form to check first: if t, ok := v.(T); ok { ... }. For multi-branch checks, switch v := i.(type) dispatches on dynamic type.