Type assertion
A type assertion is a language construct that states or checks that a value has a specified type, with runtime behavior depending on the language.
Go by Example
A runtime type assertion is a Go expression that checks and retrieves the concrete value represented by an interface value at run time.
- How it is used
- Enables handlers, wrappers, Context helpers, and database code to discover optional behavior or recover typed values.
- How deep it goes
- Demonstrates comma-ok checks, wrapper unwrapping, Context extraction, and Scanner validation without detailing assertion runtime internals.
- Examples
- Uses http.Flusher on ResponseWriter, trace IDs stored in Context, and a base64 SQL Scanner.
- What you can do after
- Apply it via Go interfaces to safely invoke optional capabilities and validate dynamically typed boundary values.
Essential TypeScript 5, Third Edition
A type assertion is a TypeScript construct that tells the compiler to treat an expression as a specified type without performing a runtime type conversion.
- How it is used
- Overrides compiler uncertainty when a value's type is known to the programmer, including union results and DOM event targets.
- How deep it goes
- Explains `as` syntax, union narrowing, rejected assertions, double assertions, and unchanged runtime values; does not present assertions as validation.
- Examples
- Uses a tax function returning strings or numbers, a task-selection answer, and a select-element quantity handler.
- What you can do after
- Apply it via TypeScript's `as` syntax to access an expected type's members while checking that the runtime value matches.
Read how Essential TypeScript 5, Third Edition explains type assertion →
Build and Orchestrator in Go (From Scratch)
A type assertion is a Go operation that extracts a value of a specified concrete or interface type from an interface value.
- How it is used
- Uses assertions to recover concrete tasks from interface-based stores during retrieval, updates, listing, and event processing.
- How deep it goes
- Explains the two-result assertion, its ok check, and failure handling, but does not descend into runtime representation internals.
- Examples
- Demonstrates assertions while storing, retrieving, updating, listing, and processing task and task-event values.
- What you can do after
- Diagnose/debug with it by checking ok and returned errors before using an interface value as its expected concrete type.
Read how Build and Orchestrator in Go (From Scratch) explains type assertion →
Go in Action, Second Edition
A type assertion is a Go operation that checks whether an interface value contains a specified concrete type and retrieves the contained value if it does.
- How it is used
- Checks interface values when generic behavior must occasionally access concrete, type-specific data.
- How deep it goes
- Explains comma-ok results, contrasts type switches, and covers exact generic-instantiation checks without broader runtime internals.
- Examples
- Uses MetricWeight inspection, measurement-system classification, and Collection[int] assertions to illustrate concrete-type handling.
- What you can do after
- Apply it via comma-ok assertions for one concrete type and type switches for several possible interface implementations.
Read how Go in Action, Second Edition explains type assertion →
Go in Practice, Second Edition
A type assertion is a Go operation that retrieves the concrete value of an interface value when it has a specified dynamic type.
- How it is used
- Uses interface assertions to test compatibility and retrieve typed values from interface-bearing APIs such as context.
- How deep it goes
- Explains assertion syntax through runnable examples and contrasts it with reflection for interface types selected at runtime.
- Examples
- Demonstrates Stringer checks on a bytes.Buffer and integer, then retrieves web configuration from context in an HTTP handler.
- What you can do after
- Apply it via Go's assertion syntax to test interface satisfaction and retrieve typed configuration from an interface value.
Read how Go in Practice, Second Edition explains type assertion →
Related concepts
- reflectionalternative to · Reflection is a programming capability for inspecting or interacting with program types, members, values, or metadata dynamically at runtime.
- typeof operatoralternative to · A typeof operator obtains type information about an operand or expression, yielding a type or a string describing its type depending on the language.
- as operatoruses · The as operator is language syntax that associates a value or expression with a specified type through assertion, casting, or conversion, depending on the language.
- sql.DBused by · sql.DB is a Go database/sql handle that provides database operations while managing a pool of database connections.
- any typeuses · The any type is a TypeScript type that permits values to be treated as having essentially any type, largely disabling static type checking for them.
- data conversioncontrasts with · Data conversion is the transformation of a value or data from one type, format, or representation to another, explicitly or implicitly.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.




