TypeScript Notları
  • Giriş
  • Handbook
    • TypeScript nedir, ne işe yarar ?
    • Kurulum ve tsc
    • Temel Tipler
    • any ve unknown
    • Union Type (Çoklu Tipler)
    • Literal Types
    • Objects (Objeler)
    • Arrays (Diziler)
    • Tuple
    • Fonksiyonlar
    • Optional Params (Opsiyonel Parametreler)
    • type
    • interface
    • readonly
    • Generics
    • Modül Yapısı
    • Type Assertion
    • keyof, typeof
    • Mapped Types
    • React ve TypeScript
      • Props Tipleri
      • State Tipleri
      • Event Tipleri
      • useRef
  • Tip and Tricks
    • json2ts
  • Kaynakça
Powered by GitBook
On this page
  1. Handbook

interface

interface sözde öğesi, tip objeleri oluşturmanın başka bir yoludur. Aşağıda fonksiyonlar ve objelerde kullanımı örneklendirilmiştir.

interface Point {
  x: number;
  y: number;
}

function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}

printCoord({ x: 100, y: 100 });
interface Teams {
    turkey: 'fenerbahce' | 'galatasaray',
    spain: 'barcelona' | 'real-madrid'
}

let champions: Teams = {turkey: "fenerbahce", spain: "barcelona"}

type ile interface aynı amaçta olsalar bile bazı farkları vardır.

1 - Fonksiyon tanımlamaları farklıdır.

interface SetPoint {
  (x: number, y: number): void;
}

type SetPoint = (x: number, y: number) => void;

2 - type ile diğer tip atamaları direk type değerine atanabilirken interface e atanamaz.

// primitive
type Name = string;

// object
type PartialPointX = { x: number; };
type PartialPointY = { y: number; };

// union
type PartialPoint = PartialPointX | PartialPointY;

// tuple
type Data = [number, string];

3 - Extend edilme şekilleri farklıdır. Ayrıca ikisi birbirinden extend edilebilir.

-Interface extends interface

interface PartialPointX { x: number; }
interface Point extends PartialPointX { y: number; }

-Type extends type

type PartialPointX = { x: number; };
type Point = PartialPointX & { y: number; };

-Interface extends type

type PartialPointX = { x: number; };
interface Point extends PartialPointX { y: number; }

-Type extends interface

interface PartialPointX { x: number; }
type Point = PartialPointX & { y: number; };

3 - interface aynı isimde birden çok tanımlanabilir fakat type tanımlanamaz.

interface Point { x: number; }
interface Point { y: number; }

const point: Point = { x: 1, y: 2 };
PrevioustypeNextreadonly

Last updated 3 years ago