0
0
Typescriptprogramming~5 mins

What types exist only at compile time in Typescript

Choose your learning style9 modes available
Introduction

Some types in TypeScript help check your code while you write it but disappear when the program runs. This keeps your code safe without slowing it down.

When you want to catch mistakes before running your code.
When you want to describe the shape of data without adding extra code.
When you want to use types for checking but not affect the final program size.
When you want to create reusable type rules that help during coding only.
Syntax
Typescript
type MyType = string | number;
interface MyInterface {
  name: string;
  age: number;
}

// These types exist only during compile time and are removed in JavaScript output.

Types and interfaces are erased after TypeScript checks your code.

They do not create any JavaScript code or affect runtime performance.

Examples
This defines a type and an interface used only to check code before running.
Typescript
type ID = number | string;

interface User {
  id: ID;
  name: string;
}
The type Point helps check the function input but disappears in the final JavaScript.
Typescript
type Point = { x: number; y: number };

function printPoint(p: Point) {
  console.log(`x: ${p.x}, y: ${p.y}`);
}
Sample Program

This program uses types and interfaces to check data shapes before running. The types do not exist in the JavaScript output.

Typescript
type Age = number;

interface Person {
  name: string;
  age: Age;
}

function greet(person: Person) {
  console.log(`Hello, ${person.name}. You are ${person.age} years old.`);
}

const user = { name: "Alice", age: 30 };
greet(user);
OutputSuccess
Important Notes

Types and interfaces are only for the developer and compiler, not for the running program.

Using these compile-time types helps find errors early without adding extra code.

Summary

Types and interfaces exist only at compile time in TypeScript.

They help catch errors and describe data shapes but do not create JavaScript code.

This keeps your final program fast and clean.