Unknown: A Better any
In this lesson, you will see the type, `unknown`.
The unknown
type is half a specific explicit type and half the type any
which allows everything. Declaring a variable as unknown
allows us to set a wide variety of types without allowing unwanted access to properties or the value of a type. The following code demonstrates that a variable with type any
can be assigned a string and then used with a function of the string
type.
Later, the variable is assigned to a number
that does not have substr
function. However, TypeScript does not catch an attempt to invoke a function that does not exist.
Get hands-on with 1400+ tech skills courses.