What is the difference between interface and type in TypeScript ?
Last Updated :
23 Sep, 2024
In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions.
Type in TypeScript
The Type System in TypeScript describes the various data types supported by the language. It is divided into three major sections: Any Type, Built-In Type, and User-Defined Type. The Type System in TypeScript is responsible for ensuring the correctness of the data types before they are used in a program.
Example: In this example we defines two TypeScript types, Geeks and MoreGeeks, and combines them using the intersection & operator. The gfg constant implements both types, storing and logging the combined object.
javascript
type Geeks = {
name: string;
age: number;
};
type MoreGeeks = {
email: string;
};
type CombinedGeeks = Geeks & MoreGeeks;
const gfg: CombinedGeeks = {
name: "kgowda",
age: 20,
email: "[email protected]"
};
console.log(gfg);
Output:
{"name": "kgowda","age": 20,"email": "[email protected]"}
Interface in TypeScript
An Interface in TypeScript is a syntactical contract that entities must adhere to. It can only contain the declaration of properties, methods, and events, without any implementation. Interfaces define a standard structure that implementing classes must follow.
Example: In this example we demonstrates interface merging in TypeScript. Two Geeks interfaces are combined automatically, allowing the gfg object to implement all properties (name, age, email) and log the merged result.
javascript
// Creating a interface
interface Geeks {
name: string;
age: number
}
interface Geeks {
email: string;
}
// Using the merged interface
const gfg: Geeks = {
name: "kgowda",
age: 20,
email: "[email protected]"
};
console.log(gfg);
Output
name: " kgowda", age: 20, email: " [email protected]"
Differences Between Type and Interface in TypeScript
Feature | Type | Interface |
---|
Definition | A collection of data types. | A syntactical contract. |
Flexibility | More flexible. | Less flexible compared to type. |
Keyw | Uses the type keyword | Uses the interface keyword. |
Naming | Supports creating a new name for a type. | Provides a way to define entities. |
Capabilities | Has fewer capabilities. | Has more capabilities. |
Object Usage | Does not inherently support the use of an object. | Supports the use of an object. |
Merged Declarations | Does not support multiple merged declarations. | Supports multiple merged declarations. |
Name Conflicts | Two types with the same name raise an exception. | Two interfaces with the same name get merged. |
Implementation | Does not have implementation purposes. | Used for implementation and extending in classes. |
Union Types | Does not support implementing or extending union types. | Supports implementing and extending union types. |
Intersection Types | Can create intersection types by combining multiple types. | Cannot create intersection interfaces. |
Usage with Primitives, Unions, and Tuples | Can be used for types like primitives, unions, and tuples. | Cannot be used with other types of declaration. |
Though TypeScript's type and interface differ in certain features, they are similar in many ways, and one does not entirely replace the other. Developers can choose which one to use based on the specific requirements of their project. The flexibility and specific use cases of both type and interface make them valuable tools in TypeScript development.
Similar Reads
TypeScript Differences Between Type Aliases and Interfaces Type In TypeScript, both type aliases and interfaces are used to define custom types, but they have distinct differences and use cases.Type Aliases: Allow the definition of types with a custom name, applicable to primitives, unions, tuples, and more complex types. Interfaces: Primarily used for defining
4 min read
Difference between interfaces and classes in TypeScript In this article, we will see what is the Difference between "interface" and "Classes" in TypeScript. Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same da
3 min read
What's the Difference Between 'extends' and 'implements' in TypeScript ? TypeScript offers powerful way for organizing code and managing relationships between different components through the use of extends and implements keywords. This article explores the distinctions and functionalities of these two keywords.ExtendsThe extends keyword is used for two main purposes in
2 min read
What are the differences between any vs Object in TypeScript? TypeScript is an open-source programming language. It is a superset of JavaScript language. TypeScript is designed for the development of large applications. any: It is a built-in data type in TypeScript which helps in describing the type of variable which we are unsure of while writing the code. Th
2 min read
Difference Between valueof and keyof in TypeScript In TypeScript, valueOf() retrieves the primitive value of an object, useful for accessing inherent data types. Conversely, keyof extracts keys as a union type, crucial for type-safe property referencing and dynamic type handling in TypeScript's type system.ValueOf() methodThe valueOf() method is a b
2 min read