0% found this document useful (0 votes)
1 views

Type & Interface ?

Interfaces in TypeScript are used to define the shape of objects and support features like declaration merging and extensibility. Type aliases, on the other hand, are more versatile, allowing for the definition of various types, including unions and intersections. The choice between using interfaces or type aliases depends on the specific needs of the code and personal preference.

Uploaded by

Wilson Arteaga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Type & Interface ?

Interfaces in TypeScript are used to define the shape of objects and support features like declaration merging and extensibility. Type aliases, on the other hand, are more versatile, allowing for the definition of various types, including unions and intersections. The choice between using interfaces or type aliases depends on the specific needs of the code and personal preference.

Uploaded by

Wilson Arteaga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Gagan Saini

Github: Gagan-Saini-GS
@gagan-saini-gs

Interface
Why Different?

Type
Created By
Gagan Saini 0
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Interface
Object Shape Definition
They are primarily used for defining the shape
of objects.

They allow you to specify the structure of an


object, including its properties and their types.

Compatibility
It’s declarations are open-ended, meaning that
you can add new properties to an interface later
without causing compatibility issues with
existing code that uses that interface.
Created By
Gagan Saini 1
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Declaration Merging
It support declaration merging, meaning that
multiple interface declarations with the same
name are merged into a single interface.

This allows you to extend the definition of an


interface across multiple declarations.

Extensibility
It support extending other interfaces, allowing
you to create more complex and reusable type
definitions by combining multiple interfaces.

Created By
Gagan Saini 2
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Code Example

Created By
Gagan Saini 3
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Type
Flexible Shape Definition
Type aliases allow you to define any kind of
type, not just objects.

They can be used to create aliases for primitive


types, union types, intersection types, & more.

Union and Intersection Types


Type aliases support defining union types and
intersection types, allowing you to combine
multiple types into a single type.

Created By
Gagan Saini 4
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Expressiveness
Type aliases are more expressive than
interfaces in some cases, especially when
dealing with complex types or conditional
types.

Readability
Type aliases can be more concise and easier to
read than interfaces, particularly for complex
types or when defining types inline.

Created By
Gagan Saini 5
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Code Example

Created By
Gagan Saini 6
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

Summary
Interfaces are typically used for defining the
shape of objects, supporting extension and
declaration merging,

Type are more versatile and can be used to


define a wide range of types, including objects,
unions, intersections, and more.

The choice between interfaces and type aliases


depends on the specific requirements of your
code and personal preference.

Created By
Gagan Saini 7
Gagan Saini
Github: Gagan-Saini-GS
@gagan-saini-gs

If found it useful then,

Like & Share

Learn What is TypeScript & it’s Features?

Created By Thanks for reading


Gagan Saini Comment Feedback Below

You might also like