Interfaces In TypeScript

[!INFO]-
topic: πŸ¦’ TypeScript
links: Common types in TypeScript
source: TypeScript course by the native web
tags: #permanent-noteΒ #published

Last Modified: =dateformat(this.file.mtime, "MMM. dd, yyyy - HH:mm")


An interface is a way to define the structure of an object in TypeScript.

Because objects behave different in JavaScript than in other object-oriented languages, there are also differences between an interface in TypeScript and e.g. C-family languages.
The name of an interface should always start with an uppercase letter.

You can also define functions in interfaces.
Optional parameters are marked with a ?.

interface Person {
	firstName: string;
	middleName?: string; // this is an optional parameter
	lastName: string;
	age: number;
	fullName: () => string; // returns string
};

const person: Person = {
	firstName: 'Tom',
	lastName: 'Smith',
	age: 44,
	fullName(): string {
		return `${this.firstName} ${this.lastName}`;
	}
};