October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How `any`, `unknown`, and `never` Work in TypeScript

TypeScript’s any opts out of checking, unknown requires narrowing before use, and never represents impossible values or exhaustive control-flow cases.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

any turns off meaningful type checking for a value, unknown accepts a value but requires you to establish its type before using it, and never represents a value that cannot occur. Use unknown for data you have not validated, reserve any for deliberate opt-outs, and use never for impossible cases and exhaustive control flow.

How the three types differ

Type Meaning What TypeScript permits Typical use
any Treat this value as unchecked. Allows arbitrary property access and calls, and lets the value flow into more specific types without verification. A deliberate escape hatch at an untyped boundary or during gradual migration.
unknown This value could be anything; establish what it is before using it. Accepts values of any type, but blocks most operations and assignments to specific types until the value is narrowed or asserted. External or dynamic input whose structure has not yet been validated.
never No value can exist here. Can be assigned to every type; no type other than never is assignable to it. Functions that do not complete and exhaustive handling of union types.

These are not simply three points on a strictness scale. any weakens checking; unknown preserves uncertainty and requires proof before use; never represents the absence of possible values. In assignment compatibility, unknown acts as a top type and never as a bottom type. any has special escape-hatch behavior; it is not assignable to never. See the TypeScript Handbook’s type compatibility rules.

As an Amazon Associate I earn from qualifying purchases.

When to use unknown

Choose unknown when a value enters your program without a type you can trust—for example, data from a dynamic source. You can receive any value as unknown, but TypeScript will not let you treat it as a string, object, or other specific type until you narrow it or assert a type. Narrowing with a runtime check is generally useful because it verifies the value before code relies on it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function describe(value: unknown): string {
  if (typeof value === "string") {
    return value.toUpperCase();
  }
  return "not a string";
}

Here, the typeof check narrows value to string inside the branch, so calling toUpperCase() is permitted. Depending on the value, narrowing can use checks such as typeof, comparisons, instanceof, or a user-defined type predicate. The Handbook’s basic types guide describes unknown as requiring this kind of checking or an assertion before use as a more specific type.

When any is appropriate—and what it costs

any is useful only when you intentionally want to opt out of checking, such as temporarily accommodating an untyped boundary during a migration. It does not mean “not yet known, but safe.” Once a value is typed any, arbitrary property access and calls can compile, and the value can be assigned to a specific type without proving that it matches. A nonexistent property can therefore pass the compiler and fail at runtime.

The Handbook describes that convenience as coming at the cost of type safety and recommends avoiding any when it is not necessary. If the value is merely unvalidated, use unknown and narrow it instead.

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 never appears

never represents a value that cannot occur. A function whose execution never completes can declare never as its return type. For example, a function that always throws does not return a value:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function fail(message: string): never {
  throw new Error(message);
}

never can also result from control-flow narrowing. If checks have ruled out every possible member of a union, no value remains in that branch. The Handbook’s explanation of never covers both non-returning functions and impossible narrowed cases.

Use never to check exhaustive unions

Assigning the remaining value to never in a final branch makes the compiler flag a newly added union variant that earlier branches do not handle. This is useful when a union is intended to be handled completely.

type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "square"; side: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case "circle":
      return Math.PI * shape.radius ** 2;
    case "square":
      return shape.side ** 2;
    default: {
      const impossible: never = shape;
      return impossible;
    }
  }
}

If a new shape is added to Shape without adding a matching case, shape in the default branch will no longer be assignable to never, exposing the missing handling at compile time. The assignment behavior is documented in the type compatibility reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Quick decision guide

  • Use unknown when a value might be anything and must be checked before use.
  • Use any only when bypassing type checking is an intentional trade-off.
  • Use never when a value or control-flow branch is impossible, or to make union handling exhaustive.

The TypeScript 3.0 release notes introduced unknown as “the type-safe counterpart of any.” That is the practical distinction: uncertainty can remain safely represented by unknown, while any discards the compiler’s protection.

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

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.

Leave a Reply

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

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.