TypeScript unknown Type

Note: If you are new to TypeScript, check out Getting Started with TypeScript tutorial first.


The unknown data type in TypeScript holds any value, but requires type verification before operations can be performed.

Here's a simple example of the unknown type. You can read the rest of the tutorial to learn more.

Example

let userInput: unknown = "Hello";

// Type checking
if (typeof userInput === "string") {
    // Now it's safe to use string methods
    console.log(userInput.toUpperCase());  
}

// Output: HELLO

Here, userInput is declared as unknown, and to perform string operations, we must first verify it's a string through type checking.


Declare an unknown type Variable

To declare a variable with the unknown type, you simply use the unknown keyword in the type annotation. For example,

let variableName: unknown;

Declaring a variable as unknown informs the compiler that the variable type is not yet known and will be determined later.


Using unknown with Function

We can also use unknown in the function argument type. For example,

function handleData(input: unknown) {
    // Check if the input is an array
    if (Array.isArray(input)) {
        console.log(input);
    }
    // Check if the input is a string
    else if (typeof input === "string") {
        console.log(input.toUpperCase());
    }
}

// Call the function with different types of inputs
handleData("programiz");          
handleData([1, 2, 3]);    

Output

PROGRAMIZ
[1, 2, 3]

Here, the function handleData() uses if-else conditions to determine the nature of input.

  • It checks if input is an array using Array.isArray(input). If true, it logs the array.
  • If input is a string, as checked by typeof input === "string", it logs the string in uppercase.

TypeScript any vs unknown

any and unknown are both TypeScript types that can hold any value, but they differ in how they enforce type safety:

any Type

It allows operations on any type without checks, which can lead to potential runtime errors. For example,

let value: any = "Hello";

// Works fine, outputs: "HELLO"
console.log(value.toUpperCase());

value = 123;

// No error at compile time, but will crash at runtime
console.log(value.toUpperCase());

unknown Type

It requires explicit type checking before operations, enhancing code safety. Consider a variable value declared as unknown.

let value: unknown = "Hello";

Since unknown is a type-safe counterpart to any, TypeScript enforces checks before performing any operations on value:

if (typeof value === "string") {
    console.log(value.toUpperCase()); 
}

Directly calling the toUpperCase() method on value would result in a compile-time error because TypeScript requires explicit confirmation of the type before performing type-specific operations.

let value: unknown = "Hello";

// Runtime Error: 'value' is of type 'unknown'
console.log(value.toUpperCase());

Using unknown prevents the runtime error.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges