Define custom types and interfaces to describe the shape of your data.
TypeScript lets you describe the shape of objects with type aliases and interface declarations.
type aliastype User = {
name: string;
age: number;
};
interfaceinterface Product {
id: number;
name: string;
price: number;
}
Both are nearly identical. type is slightly more flexible (union types, tuples), interface is preferred for object shapes by many teams.
function formatUser(user: User): string {
return `${user.name} (${user.age})`;
}
Define and export a type named Point with:
x: numbery: numberThen export a function named distance that takes two Point values and returns the Euclidean distance between them as a number.
export type Point = { ... };
export function distance(a: Point, b: Point): number {
// hint: Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2)
}
// Define and export a type named 'Point' with x and y number fields // Then export a 'distance' function that returns the distance between two Points