Typescript Interfaces vs Type alias — how, where & when to use them

javascript super-set for javascript superstars
interface Person {
first: string;
last: string;
age: number;
married: boolean; }
interface SetNumber {(x: number, y: number): void;}
// object
type Animal {
name: string;
type: string;
legs: number;
pet: boolean; }
type SetTypeNumber = (x: number, y: number) => void;// primitive
type Person = string;
// object
type Person = { first: string; age: number; };

// union
type Creation = Animal | Person;

// tuple
type arrayData = [number, string];
interface Count { x: number; }
interface Point extends Count{ y: number; }
type typeCount = { x: number; };
type Point = typeCount & { y: number; };
type interfaceTotal = { x: number; };
interface Point extends interfaceTotal { y: number; }
interface Total { x: number; }
type Point = Total & { y: number; };
interface Count {
x: number;
y: number;
}

class Totalcount implements Count {
x = 1;
y = 2;
}

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

class TotalCount2 implements Count2 {
x = 1;
y = 2;
}
// but a type based union cannot be extended or implemented
type BothCounts = { x: number; } | { y: number; };
// below code is similar to interface Point { x: number; y: number;}
interface Total { x: number; }
interface Total { y: number; }

const point: Total = { x: 1, y: 2 };

--

--

--

Software Dev, qualified lawyer, salesman, pro telemarketer, travel bug, meditator/energy healer, self help enthusiast, aspiring Tech Entrepreneur & family man

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SOLID principle #1: Single responsibility (JavaScript)

How to Convert Keynote ODT to JPG in Node.JS

Switching Cameras Based on Animation States in Cinemachine

Introducing glamorous for React Native 💄

CORS Errors- Part 2: how to solve

Twilio Proxy for masked phone numbers in Node.js

Create react app with babel and webpack 5

New features in ECMAScript 6

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
2 Minute Tech Reads

2 Minute Tech Reads

Software Dev, qualified lawyer, salesman, pro telemarketer, travel bug, meditator/energy healer, self help enthusiast, aspiring Tech Entrepreneur & family man

More from Medium

JBook, favicons and more CSS

How i reduced static files size

React: how to use the useContext hook in combination with the useReducer and firebase for user…

How to easily create JS libraries compatible with ES/AMD/UMD/CJS module systems using Nx