The 'unknown' Type
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 8 of 73.
Like any, a variable of type unknown can hold any value:
let userInput: unknown = "Hello";
userInput = 42; // No error
userInput = true; // No errorThe key difference is that TypeScript won't let you perform operations on an unknown value without first checking what type it actually is. This prevents many runtime errors:
let data: unknown = "TypeScript";
// data.toUpperCase(); // Error! TypeScript doesn't know it's a string
// You must check the type first
if (typeof data === "string") {
console.log(data.toUpperCase()); // Now it's safe!
}This makes unknown much safer than any because it forces you to verify the type before using type-specific methods or properties.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a variable named userInput with the type unknown and assign it the string "TypeScript".
Then write a type guard using typeof to check if userInput is a string. If it is a string, print the uppercase version of the string to the console. If it's not a string, print "Not a string" to the console.
This demonstrates how unknown requires type checking before you can safely use type-specific methods like toUpperCase().
Cheat sheet
The unknown type can hold any value but requires type checking before performing operations:
let userInput: unknown = "Hello";
userInput = 42; // No error
userInput = true; // No errorUnlike any, TypeScript prevents operations on unknown values without type verification:
let data: unknown = "TypeScript";
// data.toUpperCase(); // Error! TypeScript doesn't know it's a string
// You must check the type first
if (typeof data === "string") {
console.log(data.toUpperCase()); // Now it's safe!
}This makes unknown safer than any by forcing type verification before using type-specific methods.
Try it yourself
// Create the userInput variable with unknown type
let userInput: unknown = "TypeScript";
// TODO: Write your code here
// Use typeof to check if userInput is a string
// If it's a string, print the uppercase version
// If it's not a string, print "Not a string"This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples