HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to TypeScriptBasic TypesUnion & Literal TypesInterfacesTyped FunctionsArrays & TuplesGenerics & Enums
REFERENCE
📖 Overview
🖥️ Basics & Output
🏷️ Types
📐 Interface & Type Alias
🧩 Function
📋 Array & Object Types
🚀 Next
TypeScript›Interfaces

Interfaces

Defining the shape of objects (what fields, what types) and optional fields.

Simple enough for a 5-year-old to understand

Object shape with interface

You define what fields an object should have using an interface.

Example code
1
interface Person {
Define the "Person" shape.
DefinitionDefine the "Person" shape.
2
name: string;
Required text field.
DataRequired text field.
3
age: number;
Required number field.
DataRequired number field.
4
}
End of shape.
DefinitionEnd of shape.
5
const k: Person = { name: "Alex", age: 36 };
Object conforming to the shape.
VariableObject conforming to the shape.
6
console.log(k.name);
Alex
Output / InputAlex
DefinitionDataVariableOutput / Input
💡 Hint: If you miss a required field, you get an ERROR — that is the power of an interface.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write an Item interface (name: string, price: number); create a product and print ONLY the price.

🎯 Expected output: 25

Loading editor…

About this topic

An interface defines what fields and types an object possesses. In TypeScript, there are two ways to define an object shape: interface and type alias. In most cases, the two are interchangeable.

TypeScript's type system is STRUCTURAL, not nominal: if an object has the fields required by an interface, it satisfies that interface; it does not need to declare that it "implements" the interface. For those coming from Java, this is an important mental shift and makes TypeScript much more flexible.

Structural typing introduces a nuance: extra fields. When assigning a variable directly, TypeScript flags extra fields as an error, but it doesn't if you pass them through an intermediate variable. This is a deliberate design choice and feels inconsistent at first glance.

What you will learn

  • Defining an interface and matching an object to it
  • Optional and read-only fields
  • The difference between interface and type
  • Extending interfaces
  • The consequences of structural typing
  • Modeling nested objects and array fields

When to use?

Interfaces are used to model API responses, configuration objects, and component props. If an object travels through more than two places in your code, giving its shape a name with an interface serves as both documentation and protection.

If you are going to build unions, intersections, or mapped types, a type alias is required — an interface cannot do these. If you are defining an object shape and want it to be extensible, an interface is more appropriate.

Common mistakes

Wrong

interface User {
  name: string;
  email: string;
}
const u: User = { name: "Ada" };

Fields in an interface are REQUIRED by default; because email is missing, you get the error "Property 'email' is missing in type". For a field to be allowed to be missing, it must be made optional with a question mark.

Correct

interface User {
  name: string;
  email?: string;
}
const u: User = { name: "Ada" };

Wrong

interface Config {
  debug: boolean;
}
const c: Config = { debug: true, verbose: true };

On direct assignment, TypeScript catches extra properties: "Object literal may only specify known properties". This is a useful check that prevents typos — but if you assign the value to a variable first, the check does not kick in.

Correct

interface Config {
  debug: boolean;
  verbose?: boolean;
}
const c: Config = { debug: true, verbose: true };

Wrong

interface Point {
  readonly x: number;
}
const p: Point = { x: 1 };
p.x = 5;

Reassigning to a readonly field throws the error "Cannot assign to 'x' because it is a read-only property". However, readonly only protects at COMPILE time; in the generated JavaScript, there are no restrictions and the object can be modified externally.

Correct

interface Point {
  readonly x: number;
}
let p: Point = { x: 1 };
p = { x: 5 };

Frequently asked questions

Should I use interface or type?

Either works for an object shape; being consistent within the team is important. An interface can be reopened with the same name and extended, which is why it's necessary when extending library types. For unions, intersections, and mapped types, type is mandatory.

What does structural typing mean?

Whether a type conforms to another depends on its fields matching — not on its name or declaration. You don't need to say you "implement" an interface; any object with the correct fields stands in for that interface. This makes TypeScript much more flexible than Java.

Why doesn't excess property checking sometimes trigger?

The check only works when assigning an object DIRECTLY. When you pass it through an intermediate variable, the structural typing rule applies and extra fields are not a problem. It looks inconsistent but is deliberate: typos are caught on direct assignment, while flexibility is preserved during transfers.

Summary

An interface defines an object shape, and TypeScript's type system is structural — if the fields match, the type matches. Fields are required by default, readonly only protects at compile time, and excess property checking only works on direct assignments.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code