CodeBlanks Logo Icon CodeBlanks

What is a type alias?

Learn type naming with the type keyword.

What is a type alias?

A type alias gives a name to a type. You define it by writing the type keyword, the name, and the type. This makes complex types reusable and readable.

type ID = number;
let kullaniciId: ID = 42;

An alias gives a type a reusable name.

Reusing type aliases

Once defined, a type alias can be used anywhere a type is expected. It is useful for commonly used types like IDs, codes, or complex object shapes.

type ID = number;
type Isim = string;
function bul(id: ID): Isim { return "Ali"; }

An alias prevents repeated type definitions.

Practice Define and use a type alias

Define a type alias named Kimlik (number). Define a type alias named Metin (string). Declare a variable named id of type Kimlik with value 42. Print the value of id with console.log.

Starter code

index.ts
// Kimlik adında bir type alias tanımla (number)
// Metin adında bir type alias tanımla (string)
// id değişkenini Kimlik türünde tanımla, değeri 42
// id değerini yazdır

Sample solution

index.ts
type Kimlik = number;
type Metin = string;
let id: Kimlik = 42;
console.log(id);

Expected output

42

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does <code>type ID = number</code> do?

  • Names the number type Correct answer
  • Creates a variable named ID
  • Creates an interface
  • Creates a function

Explanation: The <code>type</code> keyword gives a name to a type; <code>type ID = number</code> names the <code>number</code> type as <code>ID</code> and makes it usable as a type.

2 Complete the type alias

Question
type Kimlik = ____;
let id: Kimlik = 100;
Answer
type Kimlik = number;
let id: Kimlik = 100;

Explanation: An identity number is a number; <code>type Kimlik = number</code> is the correct definition. <code>string</code> is wrong for an ID, and <code>boolean</code> and <code>void</code> make no sense as an identity number.

3 Type alias concepts

Term Meaning
type ID = number Naming a number type
type Isim = string Naming a string type
type Type naming keyword
Type alias Giving a type a reusable name

All lessons in Beginner