Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

any turns off meaningful type checking for a value; unknown accepts a value of any type but requires you to establish what it is before using it; and never represents a value that cannot occur. Use unknown for unvalidated or not-yet-described data, reserve any for deliberate opt-outs, and use never for impossible states and exhaustive control flow.

How the three types differ

These types are not simply three points on a scale of strictness. any weakens checking, unknown preserves uncertainty until you prove more, and never represents the absence of a possible value. The TypeScript Handbook describes never as “the type of values that never occur.” TypeScript Handbook: Basic Types

Type Meaning What TypeScript permits Typical use
any “Treat this as unchecked.” Arbitrary property access and calls, and broad assignment into more specific types without verification. A deliberate escape hatch at an untyped boundary or during gradual migration.
unknown “This could be anything; establish its type before using it.” Accepts values of any type, but most operations and assignment to a specific type require narrowing or an assertion. External or dynamic input whose structure has not yet been validated.
never “No value can exist here.” Assignable to every type, while no type other than never is assignable to it. Non-returning functions and exhaustiveness checks.

The Handbook’s type-compatibility rules describe unknown as a receiving top type and never as a bottom type for assignment. any has special escape-hatch behavior: it can flow into specific types without checks, but it is not assignable to never. These assignment rules do not make any safe. Type Compatibility

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When to use unknown

Choose unknown when a value comes from a dynamic or external source and its shape has not yet been established. Unlike any, it prevents you from assuming that a property exists or that a value supports a particular operation. First narrow it with a runtime check; TypeScript then allows operations justified by that check.

function describe(value: unknown): string {
  if (typeof value === "string") {
    return value.toUpperCase();
  }
  return "not a string";
}

Here, typeof value === "string" narrows the value inside the branch, so calling toUpperCase() is valid. Depending on the value, useful narrowing tools include typeof, comparisons, instanceof, and user-defined type predicates. TypeScript Handbook: Basic Types TypeScript 3.0 Release Notes

TypeScript 3.0 introduced unknown as the type-safe counterpart to any. It still accepts values of any type; the difference is that the compiler makes you narrow or assert the value before treating it as a more specific type. TypeScript 3.0 Release Notes

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

When any is appropriate

Use any only when opting out of checking is intentional—for example, as a temporary bridge while migrating untyped code or when interacting with an untyped boundary that you cannot yet describe. It lets unchecked assumptions spread: a property access, method call, or assignment to a specific type may compile even though it is invalid at runtime. The Handbook warns that this convenience comes at the cost of type safety and recommends avoiding any when it is unnecessary. TypeScript Handbook: Basic Types

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When never is appropriate

Functions that do not return

A function whose execution cannot complete normally can return never, such as one that always throws:

function fail(message: string): never {
  throw new Error(message);
}

The type communicates that callers cannot receive a normal return value from this function. An endless loop is another case where a function does not return.

Exhaustive checks for unions

never is also useful after control-flow narrowing has ruled out every possible member of a union. Assigning the remaining value to never makes an unhandled case visible to the compiler if the union later gains another variant. The Handbook also notes that never can result from a type guard that cannot be true. TypeScript Handbook: Basic Types

A practical choice

  • Use unknown when you have a value but have not yet validated its type.
  • Use any only when bypassing type checks is a deliberate decision.
  • Use never to mark a path that cannot produce a value or to make union handling exhaustive.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.