Search⌘ K

Type Checking with typeof

Explore how TypeScript's typeof operator enables borrowing types from variables and aids in narrowing types through control flow analysis. Understand its limitations with classes and interfaces, and prepare to learn alternative type-checking strategies.

We'll cover the following...

Borrowing type

Borrowing the type of another variable is possible by using typeof. Borrowing type brings protection at design time by allowing you to set a type without defining an actual interface or type. For example, you define a variable with some members using curly braces. There is no actual way to get the schema of the object.

However, with typeof, you can extract the type from the variable. The extraction is possible for variables or parameters. Instead of a concrete type, the variable uses typeof followed by the name of the variable to borrow the ...