Difference between unknown and any keyword in Typescript

In TypeScript, both unknown and any are types that allow values of any type, but they have different type safety characteristics and use cases. Understanding the difference helps you write safer and more predictable code.

1. any

  • Type: Represents any value without restrictions.
  • Type Safety: Disables type checking completely. Using any bypasses TypeScript’s compile-time checks, which can lead to runtime errors.
  • Use Case: Use when you want to allow any type and do not need type checking (e.g., when migrating JavaScript code to TypeScript).
let anything: any = "Hello";
anything = 42;  // No error
anything.toUpperCase();  // No error, but could cause runtime issues if `anything` is not a string

2. unknown

  • Type: Represents a value of an unknown type.
  • Type Safety: Maintains type safety by requiring type assertions or type narrowing before using the value.
  • Use Case: Use unknown when the type is uncertain but you want to enforce type checking before performing operations.
let value: unknown = "Hello";
value = 42;  // Allowed

// TypeScript enforces type checking before using `value`
if (typeof value === "string") {
  value.toUpperCase();  // Safe because the type has been checked
}

// Direct usage without type assertion causes a compile-time error
// value.toUpperCase();  // Error: Object is of type 'unknown'

Key Differences

Featureanyunknown
Type CheckingDisabledEnabled (requires type narrowing)
SafetyUnsafeSafe
Use in OperationsNo restrictionsRequires type checking or assertions
Use CaseFor dynamic/mixed content, quick prototypingWhen type is uncertain but type safety is needed

Example Comparison

function handleAny(input: any) {
  console.log(input.toFixed(2));  // No error, potential runtime issue if input is not a number
}

function handleUnknown(input: unknown) {
  if (typeof input === "number") {
    console.log(input.toFixed(2));  // Safe after type check
  } else {
    console.log("Not a number");  // Prevents runtime errors
  }
}

Summary

  • Use **any** if you want to disable type checking and allow any operation.
  • Use **unknown** for better type safety, requiring explicit checks or assertions before using the value.

In modern TypeScript, unknown is generally preferred over any for safer and more predictable code.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *