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.
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:
#1 Best Overall
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 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.
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.
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.
Quick Recap
Best Value
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.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →




