frameworks/typescript

[typescript]interface

iliosncelini 2018. 12. 6. 15:11



























1
2
3
4
5
6
7
8
9
10
11
interface Person{
    name:string;
    [index:string]:string;
}
 
const person:Person={
    name:'mark'
    indexableVar:'test'
}
 
 
cs


[index:string]에서 string 자리에 올수 있는건 number혹은 string 뿐이다.
















































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
interface Iperson{
    name: string;
    hello(): void;
}
 
class Person implements Iperson{//Person클래스에서 프로퍼티 name과 hello()는 반드시 구현되어야한다.
    name = null;
 
    constructor(name: string) {
        this.name = name;
    }
 
    hello(): void{
        console.log(`hello, my name is ${this.name}`);
    }
 
    hi(): void{
        console.log(`hi, my name is ${this.name}`);
    }
}
 
const p1:Person = new Person('minsu');
const p2:Iperson = new Person('sooji');
 
p1.hello();
p1.hi();
 
p2.hello();//p2는 Iperson클래스 이므로 hi()를 가지고 있지 않다.
cs