TypeScript Differences Between Type Aliases and Interfaces Type (original) (raw)

Last Updated : 23 Jan, 2025

In TypeScript, both type aliases and interfaces are used to define custom types, but they have distinct differences and use cases.

Type Aliases

Type aliases in TypeScript allow you to create custom names for types, enhancing code readability and reusability.

JavaScript ``

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

function printPoint(point: Point): void { console.log(x: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>p</mi><mi>o</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi><mi>x</mi></mrow><mo separator="true">,</mo><mi>y</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">{point.x}, y: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="mord mathnormal">o</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mord">.</span><span class="mord mathnormal">x</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{point.y}); }

const myPoint: Point = { x: 10, y: 20 }; printPoint(myPoint);

``

**Output:

x: 10, y: 20

TypeScript Interfaces

Interfaces in TypeScript define the structure of an object, specifying the required property types and method signatures. They ensure type safety and enhance code clarity.

JavaScript ``

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

function displayPoint(point: Point): void { console.log(x: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>p</mi><mi>o</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi><mi>x</mi></mrow><mo separator="true">,</mo><mi>y</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">{point.x}, y: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="mord mathnormal">o</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mord">.</span><span class="mord mathnormal">x</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{point.y}); }

const myPoint: Point = { x: 15, y: 25 }; displayPoint(myPoint);

``

**Output:

x: 15, y: 25

Type Aliases vs Interfaces in TypeScript

Type Aliases Interfaces Type
Type Aliases use the type keyword to define a new type. Interface Type use the interface keyword to define a new type.
Type aliases support extending other type aliases by intersection type. The & symbol is used to create an intersection type. Type aliases also extends interface types. Interface Type also supports extending other interfaces by using the `extends` keyword. Interface type also extends type aliases.
Type Aliases can be implemented by class with the use of the `implements` keyword. but they cannot be extended or implemented. Interface Type can also be implemented by class with the use of the implements keyword.
Type Aliases don't support declaration merging where declaring the same type of aliases again a second time with another type gives an error. Interface type does support declaration merging where declaring the same interface again a second time merges with previous attributes of the last type interface.