TypeScript Survival Guide

Favorite typescript videos

093 — Interfaces (IsPerson contracts on objects and function parameters)

Full companion narrative for TypeScript Tutorial #15 — interfaces as structure enforcement: the IsPerson contract, excess-property errors, reuse across objects, and interface-typed function parameters

Video: 093 · TypeScript Tutorial #15 - Interfaces · watch · ~07:18 · Net Ninja TypeScript tutorial series, lesson 15 (Shaun)

Source: this is a written companion to the video. Part of the Favorite TypeScript Videos curated path, module 05 (objects and interfaces). The one idea: an interface enforces structure — it never creates objects.

the contract, not the thing

“OK then gangs so another tool that we can use in TypeScript that we can’t in JavaScript is an interface Now an interface allows us to enforce a certain structure of a class or an object We can use it to describe what properties and what methods and what the types of those properties and return types of those methods are.”

interface IsPerson {
	name: string;
	age: number;
	speak(a: string): void;
	spend(a: number): number;
}

No interface keyword exists in JavaScript — the whole construct evaporates at compile time, leaving only the checked shapes behind.

IsPerson declared with properties and method signatures

interface walkthrough in the editor

an object earns the annotation member by member

“So for example say I have A constant called me and I’m gonna set that type to be is person like that and initialize it with a value OK.”

const me: IsPerson = {
	name: "Shaun",
	age: 30,
	speak(text: string): void {
		console.log(text);
	},
	spend(amount: number): number {
		console.log("I spent", amount);
		return amount;
	},
};

The annotation demands every member with matching types, and the error clears only once all four are present — parameter names may differ (text for a), but count, types, and return types must line up.

me annotated as IsPerson

all four members satisfying the contract

extra properties are rejected

“Now what if I add on something else an extra property for example skills Well if I set this to be an array we get an error and that’s because this skills property right here is not inside the interface It now no longer matches the structure of the interface.”

const me: IsPerson = {
	// ...all four members,
	skills: [], // type error: not in the interface
};

The excess-property check is the enforcement made visible: the object must match the contract exactly, no more and no less.

the excess-property error on skills

one contract, many objects — including parameters

“Now the benefit of this is that we can have multiple different objects that is of type is person that implements this interface but they could have different values the methods could be slightly different they all have to be the same signature where they take in a string or a Number.”

let someone: IsPerson;

const greetPerson = (person: IsPerson) => {
	console.log(`hello, ${person.name}`);
};

“Another good thing is that we can say inside a function if we take a parameter it must be an is person type So we could say for example we have a function called greet Person.” Typing the parameter as IsPerson moves the shape check to the call site — any conforming object is accepted, and the function body can trust person.name exists.

a second IsPerson object with different values

greetPerson taking an IsPerson parameter

Prev: 092 — Modules · Next: 094 — Interfaces with Classes

The auto-transcript is clean on the interface mechanics but loose on the identifier; this narrative uses the corrected form IsPerson in PascalCase (transcript “is person” / “IPerson” in early frames). Method implementations use their own parameter names (text, amount), which need not match the signature names (a).