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.
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.