Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Check Whether an Object Matches an Interface in TypeScript

TypeScript checks interface compatibility at compile time. For runtime data, use a type guard that verifies each required property and value.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You cannot check an arbitrary object against a TypeScript interface at runtime: interfaces are erased when JavaScript is emitted. TypeScript checks whether values match an interface while compiling; to validate runtime data, write a type guard that checks the required properties and their values.

What “implements an interface” means in TypeScript

TypeScript uses structural typing: a value is compatible with an interface when it has the required members with compatible types. An object does not need to declare that it implements an interface.

As an Amazon Associate I earn from qualifying purchases.

interface User {
  id: number;
  name: string;
}

const candidate = { id: 1, name: "Ada" };
const user: User = candidate; // TypeScript checks compatibility

The compiler reports an error if a required member is missing or has an incompatible type. This is a compile-time check, not a test performed on the object while the program runs. See the TypeScript handbook’s Type Compatibility and Interfaces documentation.

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

What a class’s implements clause does

A class can use implements to ask the compiler to check that its instance side satisfies an interface:

interface Runnable {
  run(): void;
}

class Job implements Runnable {
  run() {}
}

If the class lacks a required member or gives it an incompatible type, the compiler reports an error. The clause does not add runtime interface information or change the class’s inferred type; it is a compile-time check. See Implements Clauses.

How to validate an object at runtime

For JSON, API responses, user input, or other values whose shape is not guaranteed by your TypeScript source, accept the value as unknown and check every required property before treating it as the interface.

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
interface User {
  id: number;
  name: string;
}

function isUser(value: unknown): value is User {
  return typeof value === "object"
    && value !== null
    && "id" in value
    && typeof value.id === "number"
    && "name" in value
    && typeof value.name === "string";
}

const input: unknown = JSON.parse('{"id":1,"name":"Ada"}');

if (isUser(input)) {
  console.log(input.name); // input is narrowed to User here
}

The return annotation value is User tells TypeScript how to narrow the value when the function returns true. It does not generate or verify the checks for you: the function is trustworthy only to the extent that its runtime conditions really enforce the contract. For more on predicates and narrowing, see the handbook’s Narrowing and Advanced Types documentation.

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

Check property values, not just property names

The in operator can help narrow a value based on whether a property exists, but presence alone does not establish that the value has the expected type. The guard above checks that id is a number and name is a string as well as checking that both properties exist. A guard for a larger interface should validate every required member that matters to the contract.

When to use assignment, a guard, or instanceof

Approach When it runs What it checks Use it for
Assignment to an interface or a class implements clause During TypeScript type checking Structural compatibility of members Source code checked by the TypeScript compiler
Custom type predicate At runtime, with compiler narrowing afterward Only the conditions written in the guard Untrusted values such as parsed JSON, API responses, or user input
instanceof At runtime Whether an object’s prototype chain matches a constructor’s prototype Class instances and built-in constructors such as Date

Use instanceof with an actual runtime constructor—for example, value instanceof Date—not an interface. TypeScript interfaces do not exist as JavaScript constructors, so value instanceof SomeInterface is not a valid way to test an interface. The handbook explains instanceof narrowing and notes the erasure of TypeScript types.

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

Why a type assertion is not validation

A type assertion such as value as User changes what the compiler assumes about a value; it does not check the value’s properties at runtime. Use an assertion only when you already have a separate reason to trust the value. When data is unknown, validate it with a guard before relying on the interface.

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.