1interface IEmployee {
2 empCode: number;
3 empName: string;
4 getSalary: (number) => number; // arrow function
5 getManagerName(number): string;
6}
7
1interface IPerson {
2 name: string
3 age: number
4 hobby?: string[]
5}
6
7class Person implements IPerson {
8 name: string
9 age: number
10 hobby?: string[]
11
12 constructor(name: string, age: number, hobby: string[]) {
13 this.name = name
14 this.age = age
15 this.hobby = hobby
16 }
17}
18
19const output = new Person('john doe', 23, ['swimming', 'traveling', 'badminton'])
20console.log(output)
21
1// https://stackoverflow.com/a/41967120
2// `I` Prefix not recommended :
3// Ref : https://stackoverflow.com/a/41967120,
4// https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
5interface SquareConfig {
6 color?: string;
7 width?: number;
8 [propName: string]: any;
9}
10
11interface StringArray {
12 [index: number]: string;
13}
14
15let myArray: StringArray;
16myArray = ["Bob", "Fred"];
17
18let myStr: string = myArray[0];
19
1//INTERFACE TYPE
2interface Animal { type Animal = {
3 name: string; name: string;
4} }
5interface Bear extends Animal { type Bear = Animal & {
6 honey: boolean; honey: Boolean;
7} }
8
9const bear = getBear(); const bear = getBear();
10bear.name; bear.name;
11bear.honey; bear.honey;
1interface LabeledValue {
2 label: string;
3}
4
5function printLabel(labeledObj: LabeledValue) {
6 console.log(labeledObj.label);
7}
8
9let myObj = { size: 10, label: "Size 10 Object" };
10printLabel(myObj);Try
11