ProgramGuru

Introduction to Interfaces

Understand Introduction to Interfaces

An interface defines a structural contract for object-like values, listing properties and methods that must be available.

What you will learn: the purpose of introduction to interfaces, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how introduction to interfaces works and where its type rules apply.
  • Predict the exact output of the introduction to interfaces example.
  • Diagnose a common issue: Do not write user instanceof User, because User is only a type.

Build the mental model

Implementation is implicit: an object matches an interface when it has compatible required members. Interfaces can extend other interfaces and may be merged by declarations. They are compile-time constructs, not runtime classes.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Introduction to Interfaces: explained visuallyInterface User → id: number → name: stringInterface Userid: numbername: stringFollow the values through the operation
Introduction to Interfaces: Interface User → id: number → name: string.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

interface User {
  id: number;
  name: string;
}
const user: User = { id: 8, name: "Ravi" };
console.log(user.name);
console.log(user.id);

Verified output

Ravi
8

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

The object literal includes both required fields and satisfies User. The runtime logs Ravi and 8; there is no User instance constructor.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not write user instanceof User, because User is only a type. Check runtime input with actual JavaScript guards when necessary.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Create an interface Movie with title and year. Construct an object matching it and deliberately omit year to see the compiler error.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

How does an object satisfy an interface?
Choose one answer. You can try again.

Answer and explanation

By having compatible required members.

An interface defines a structural contract for object-like values, listing properties and methods that must be available.

Review the key idea

Introduction to Interfaces: An interface defines a structural contract for object-like values, listing properties and methods that must be available.

The object literal includes both required fields and satisfies User. The runtime logs Ravi and 8; there is no User instance constructor.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Interface Properties and Methods →