Type Checking with typeof
This lesson goes over type checking with JavaScript's typeof.
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 schema. This technique is called “type aliases”.
The transposition of the returned type from typeof
can be in a concrete type by using the type
keyword, followed by the name of the type you create by extracting this one from typeof
.
Get hands-on with 1400+ tech skills courses.