TS PlaygroundLearnJavaScriptInterfaces & Object Types
javascript/beginner

Interfaces & Object Types

Define the shape of objects with interfaces and type aliases.


Describing object shapes

TypeScript lets you describe the exact shape an object must have using an interface or a type alias.

Interface

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

const user: User = { name: "Alice", age: 30 };

An interface is like a contract. If you forget a field or use the wrong type TypeScript will tell you immediately.

Optional fields

Add ? to mark a field as optional:

interface Product {
  id: number;
  name: string;
  description?: string; // optional
}

Type aliases

type does the same thing and is often interchangeable with interface:

type Point = {
  x: number;
  y: number;
};

Your task

Define and export a User interface with:

  • name: string
  • age: number
  • email?: string (optional)

Then create and export a user object that matches it, with at least name and age filled in.

// Define an exported interface named 'User' with:
//   name: string
//   age: number
//   email?: string  (optional)
//
// Then export a 'user' object that satisfies the interface.

Click Run Tests to see results
Types & InterfacesArrays & Tuples