Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials TypeScript Types & Interfaces
TypeScript Intermediate FREE

Types & Interfaces

Lesson 1 of 17 Intermediate Interactive

TypeScript provides several basic types: string, number, boolean, array, tuple, enum, any, void, null, undefined.

Interfaces define the shape of an object.

Syntax

TYPESCRIPT
interface User {
    name: string;
    age: number;
    email?: string;
}

const user: User = {
    name: "John",
    age: 30
};
Interfaces and Type Annotations
TYPESCRIPT
interface User {
    id: number;
    name: string;
    email: string;
    age?: number;
}

const user: User = {
    id: 1,
    name: "Alice",
    email: "alice@example.com"
};

function showUser(u: User): void {
    console.log(`${u.name} (${u.email})`);
}

showUser(user);

Practice

1
Exercise

Create an interface for a Product with name and price.

Answer
interface Product {
    name: string;
    price: number;
}

Quick Quiz

1

Which keyword defines an interface in TypeScript?

The "interface" keyword defines the shape of an object in TypeScript.

Interview Questions

Both define types, but interfaces can be extended and implemented. Types are more flexible for unions and intersections.