CodeBlanks Logo Icon CodeBlanks

Type-only import

Learn type-only exports.

Type-only import

The import type syntax imports items that will be used only as types. These items do not exist at runtime; they are fully erased at compile time. Since they are removed from the generated JavaScript, the bundle size stays small.

// kisi.ts
export interface Kisi {
  ad: string;
}

// app.ts
import type { Kisi } from "./kisi";
function goster(k: Kisi) {
  return k.ad;
}

When to use type-only?

If you only need an interface, type alias, or type parameter from a module, you should use import type. This tells the compiler the item is not needed at runtime. Especially under isolatedModules, type-only items may be required to be imported with import type.

Practice Export a type and use it

Export interface Kisi with properties ad: string and yas: number. Export function goster with signature (k: Kisi): string and body return k.ad + k.yas. Print the result of goster({ ad: "Ece", yas: 25 }) with console.log.

Starter code

index.ts
// Kisi arayüzünü dışa aktar: ad string, yas number
// goster fonksiyonunu dışa aktar, Kisi parametreli
// ad ve yas değerlerini birleştirip döndürsün
// goster'i nesneyle çağır, sonucu yazdır

Sample solution

index.ts
export interface Kisi {
  ad: string;
  yas: number;
}

export function goster(k: Kisi): string {
  return k.ad + k.yas;
}

console.log(goster({ ad: "Ece", yas: 25 }));

Expected output

Ece25

Questions and answers

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

1 How can an item imported with <code>import type</code> be used?

  • Only in type positions Correct answer
  • Both as a type and as a value
  • Only as a value
  • Accessible at runtime

Explanation: An item imported with import type can only be used in type positions; it cannot be accessed as a value. It is fully erased at runtime, so value access is a compile error.

2 Add type

Question
import ____ { Kisi } from "./kisi";
Answer
import type { Kisi } from "./kisi";

Explanation: For a type-only import you write import type. only, interface, and as are not valid keywords in this position; only type carries this meaning.

3 Type-only import parts

Term Meaning
import type Type-only import
export interface Kisi Type export
from "./kisi" Module path
k: Kisi Type position usage

All lessons in Intermediate