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
1interface Foo {
2 bar: string;
3 qux: number;
4}
5// Creates object implementing interface:
6const MyFoo = <Foo> {
7 bar: "Hello",
8 qux: 7
9}
10// Or:
11const MyFoo: Foo = {
12 bar: "Hello",
13 qux: 7
14}
1type ErrorHandler = (error: IError) => void // type for only one function
2// or
3interface IErrorHandler {
4 ErrorHander: (error: IError) => void
5}
6
7// IError Interface if interest
8interface IError {
9 error: string;
10 status: number;
11 message: string;
12}
1interface Task{
2 name: String; //property
3 run(arg: any):void; //method
4}
5
6class MyTask implements Task{
7 name: String;
8 constructor(name: String) {
9 this.name = name;
10 }
11 run(arg: any): void {
12 console.log(`running: ${this.name}, arg: ${arg}`);
13 }
14}
15
16let myTask: Task = new MyTask('someTask');
17myTask.run("test");