Skip to content

Type Aliases and Interfaces

Jorge Castro edited this page Nov 10, 2025 · 1 revision

Without aliases nor interfaces

function printCoord(pt: { 
  name: string;
  x: number;
  y: number;
  z: number 
}) {
  console.log(`${pt.name} has coordinates (${pt.x}, ${pt.y}, ${pt.z})`);
}

function calculateDistance(
  pt1: {
    name: string;
    x: number;
    y: number;
    z: number;
  },
  pt2: {
    name: string;
    x: number;
    y: number;
    z: number;
  }
) {
  const dx = pt2.x - pt1.x;
  const dy = pt2.y - pt1.y;
  const dz = pt2.z - pt1.z;
  return Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2);
}

With a type alias

type Point3D = {
  name: string;
  x: number;
  y: number;
  z: number;
};

Or with an interface

interface Point3D {
  name: string;
  x: number;
  y: number;
  z: number;
};

Using the alias

function printCoord(pt: Point3D) {
  console.log(`${pt.name} has coordinates (${pt.x}, ${pt.y}, ${pt.z})`);
}

function calculateDistance(pt1: Point3D, pt2: Point3D) {
  const dx = pt2.x - pt1.x;
  const dy = pt2.y - pt1.y;
  const dz = pt2.z - pt1.z;
  return Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2);
}

Clone this wiki locally