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

TypeScript checks whether an object matches an interface at compile time, based on its members. It does not retain interfaces in JavaScript, so you cannot use instanceof to test an arbitrary object against one at runtime. For external or otherwise untrusted data, write a runtime guard that checks the properties and values your interface requires.

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 literal does not need to declare that it implements an interface.

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

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

If a required member is missing or has an incompatible type, the compiler reports an error. This is a source-code check; it does not inspect data later received by the running program. See the TypeScript handbook’s Type Compatibility and Interfaces guides.

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

When to use a class implements clause

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

interface Runnable {
  run(): void;
}

class Job implements Runnable {
  run() {}
}

If Job omits run or gives it an incompatible type, TypeScript reports an error. The clause is a compile-time check: it does not add interface information to the class at runtime or change how the class’s members are inferred. Read Implements Clauses for the handbook’s details.

How to check an object at runtime

For JSON, API responses, user input, or other values whose shape has not been checked by the compiler, accept the value as unknown and validate it. A user-defined type guard can then tell TypeScript what the successful checks establish:

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 guard checks both that the required properties exist and that their values have the required types. Add checks for every required member in your actual interface, including nested values if the contract requires them. The predicate annotation does not verify that the function is correct; TypeScript trusts the programmer’s declared predicate. The handbook explains narrowing and user-defined type predicates.

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

What the in operator can and cannot establish

A check such as "id" in value can help narrow a value by property presence. By itself, it does not prove that id is a number or that every other required member is valid. Optional properties and members shared by different object types can also leave more than one possibility. Check member values explicitly when validating a complete contract; see the handbook’s section on the in operator.

Why instanceof does not work with interfaces

Interfaces are erased when TypeScript emits JavaScript: there is no interface constructor available for a runtime test. Consequently, value instanceof SomeInterface is not a valid way to check an interface. A type assertion such as value as User is not a substitute—it changes what the compiler assumes, but performs no runtime validation. The handbook describes type erasure and the lack of runtime type information.

Use instanceof when the target is an actual constructor-backed class or built-in, such as Date. It tests the JavaScript prototype chain, not whether an arbitrary object has an interface’s shape. See the handbook’s explanation of instanceof.

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

Which check should you use?

Approach When it runs What it establishes Use it for
Assignment to an interface or a class’s implements clause Type checking Structural compatibility of members Source code checked by TypeScript
Custom type predicate At runtime, then compiler narrowing Only the conditions implemented in the guard Untrusted values such as API data or user input
instanceof At runtime Relationship to a constructor’s prototype chain Class instances and built-in constructors

A compile-time compatibility check does not validate external data once the program is running. Conversely, a runtime guard only establishes the conditions it actually tests.

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.

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.