Type Alias & Interface

TYPE ALIAS

(Custom named types)

// Type aliases allow us to give a type a name
type StringOrNumber = string | number;
type DbStatus = 1 | 2 | 3 ;

Note: TYPE ALIAS can do more than interface (But for simplicity skipping it)


INTERFACE

Can extend from other interfaces

interface Patients extends CommonProperties {
Name: string;
}

Note: Interfaces can also extend classes Docs

Describing function signatures

interface myEventHandler {
(eventObj: any, message: string): void;
}
const evtButtonClick: myEventHandler = (_contact, _message) => {
};
//now no need to specify types for function params
//as they are inherited from function signatures type

Note: Can do it in type alias also

type myEventHandler2 = (eventObj: any, message: string) => void;

Describing constructor signatures

interface ContactConstructor {
new (...args: any[]): HasEmail | HasPhoneNumber;
}

Sample

Nested object

interface Address {
home: {
areaCode: number;
num: number;
};
streetName?: string;
}