DEV Community

Md Ismail Ahammed Roman
Md Ismail Ahammed Roman

Posted on

What are some differences between interfaces and types in TypeScript?

What is TypeScript?

TypeScript is a high-level programming language developed by Microsoft. It adds static typing to JavaScript and is considered a superset of JavaScript. When programmers create large-scale projects with JavaScript, it can be difficult to understand what type of data is being passed, since JavaScript is a loosely typed language. This is why TypeScript has become popular.

When a programmer writes code in TypeScript, the compiler shows an error if the wrong type is used. It helps catch type-related errors during development, improving productivity and reducing bugs, making it easier to build more reliable and maintainable projects.

What are some differences between interfaces and types in TypeScript?
In TypeScript, a type is used to define the kind of data a variable can hold.
When programmers write code like

let name: string = 'ismail'; // string
let value: number = 111; // number
let isAdmin: boolean = true; // boolean

BUT TYPESCRIPT IS NOT THE RIGHT WAY TO WRITE CODE.

let names:string='ismail'///string
let value:number=111;// number
let isAdmin:boolean= true||false;// boolean

But it's not reusable. If you need the same structure in multiple places, you can define a custom type:
`


type UserDetails = {
name: string;
age: number;
};

Now we can access and reuse this code. type user
interface and type are same syntax interface assign with assing oparetor and type use assing oparetor
`
let userInfo: UserDetails = {
name: "ismail",
age: 29
};

Difference Between Type and Interface

// Using type
type User = {
name: string;
age: number;
};

// Using interface
interface User2 {
name: string;
age: number;
}
An interface extending person
interface Person {
name: string;
}
interface Employee extends Person {
salary: number;
}`

type cannot be the same interface as mage
type use union, primitive, and tuple, but interface only object

Top comments (0)