xxxxxxxxxx
interface IPerson {
name: string
age: number
hobby?: string[]
}
class Person implements IPerson {
name: string
age: number
hobby?: string[]
constructor(name: string, age: number, hobby: string[]) {
this.name = name
this.age = age
this.hobby = hobby
}
}
const output = new Person('john doe', 23, ['swimming', 'traveling', 'badminton'])
console.log(output)
xxxxxxxxxx
export interface IBaseEntity {
id: string
}
export interface IBaseEntityClass {
new(_id?: string, _data?: any): IBaseEntity
}
class Test implements IBaseEntity {
id: string
constructor(_id?: string, _data?: any) {
this.id = 'MOCK_ID'
}
}
let baseEntityClass: IBaseEntityClass = Test; // The class test fulfills the contract of IBaseEntityClass
new baseEntityClass("", {}) // constructing through IBaseEntityClass interface
xxxxxxxxxx
type ErrorHandler = (error: IError) => void // type for only one function
// or
interface IErrorHandler {
ErrorHander: (error: IError) => void
}
// IError Interface if interest
interface IError {
error: string;
status: number;
message: string;
}
xxxxxxxxxx
/*
In TypeScript, you can create your OWN types and use them the
same way that you would primitive types like numbers and strings.
One way to do this is by creating an interface.
An interface in TypeScript is a data structure that defines the shape of data.
Let’s see this in action:
*/
interface Order {
customerName: string,
itemNumbers: number[],
isComplete: boolean
}
/*
The interface keyword is used to initialize an interface,
which shows us the SHAPE of the data that’s coming.
Think of an interface like a factory mold.
This interface is used to stamp out Order types for a store.
Now let’s actually use the Order interface to type a variable:
*/
let order1: Order;
order1 = {
customerName: "Abiye",
itemNumbers: [123,44,232],
isComplete: false
}
/*
Let’s analyze the order1 variable.
It is of an "Order" type, so it must have 3 fields:
the first field is a string, the second field is an array of integers,
and the third field is a boolean. It MUST have each of those fields in order to
fulfill the contract of the interface. Try omitting one of the fields in
order1 (for example, remove the customerName).
You will receive an error because the contract has not been fulfilled.
*/
/*
An interface contract is simply the list of fields in that interface
that any variable needs if it wants to use that type.
All of the normal fields within an interface must be implemented in any
variable that uses that type.
/*
xxxxxxxxxx
//INTERFACE TYPE
interface Animal { type Animal = {
name: string; name: string;
} }
interface Bear extends Animal { type Bear = Animal & {
honey: boolean; honey: Boolean;
} }
const bear = getBear(); const bear = getBear();
bear.name; bear.name;
bear.honey; bear.honey;
xxxxxxxxxx
interface LatLng {
lat(): number;
lng(): number;
}
interface LatLngConstructor {
new(lat: number, lng: number): LatLng;
}
declare var LatLng: LatLngConstructor;
xxxxxxxxxx
interface LabeledValue {
label: string;
}
function printLabel(labeledObj: LabeledValue) {
console.log(labeledObj.label);
}
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);Try
xxxxxxxxxx
interface Person {
name: string;
age: number;
}
function greet(person: Person) {
return "Hello " + person.name;
}
Try
xxxxxxxxxx
interface Person {
name: string;
age: number;
}
function greet(person: Person) {
return "Hello " + person.name;
}
Try