root@coding-prodigies:~# โ–Š
// lesson 2 of 12 ยท 16 min

Basic types and interfaces

โš‘ Report an issue with this lesson
let title: string = "HTML Fundamentals";
let price: number = 29.99;
let isFree: boolean = false;

interface Course {
  title: string;
  price: number;
  level: "beginner" | "intermediate" | "advanced";
}

const course: Course = { title: "SQL Databases", price: 59.99, level: "intermediate" };

An interface describes the shape an object must have. TypeScript checks it at compile time โ€” pass an object missing price, and your editor flags it before you ever run the code.

TypeScript's type system is structural, not nominal -- it checks whether an object has the right shape, not whether it was explicitly declared as that interface. Two differently-named interfaces with identical fields are interchangeable:

interface Point { x: number; y: number; }
interface Coordinate { x: number; y: number; }

function distance(p: Point): number {
  return Math.sqrt(p.x ** 2 + p.y ** 2);
}

const c: Coordinate = { x: 3, y: 4 };
distance(c); // fine -- c has the right shape, even though it's typed as Coordinate

type aliases are a close alternative to interface for object shapes:

type CourseType = {
  title: string;
  price: number;
};

For plain object shapes, the two are nearly interchangeable. The practical difference: interface can be re-opened later and extended with more fields via a second declaration of the same name (useful for augmenting types from a library), while type cannot -- but type can express things interfaces can't, like unions ("a" | "b") and mapped types. A common convention is interface for object shapes you expect to extend, and type for everything else.

Arrays and tuples round out the basics:

let tags: string[] = ["css", "layout"];
let pair: [string, number] = ["price", 29.99]; // tuple -- fixed length, fixed types per position

Try it yourself

Exercise: Define a Course interface with title (string) and price (number), then create one and log it.
Expected output: open-ended โ€” there's no single correct output here, just get your code running without errors.
typescript
Output

      
    

Run your code and get it working before marking this lesson complete.

// free preview

9 more lessons โ€” including Capstone project: typed API client โ€” plus a certificate are waiting.

Unlock the full course โ€” $149.99