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








'frameworks > typescript' 카테고리의 다른 글

[typescript]iterator  (0) 2018.12.11
[typescript]generic(+additional:string literal type)  (0) 2018.12.07
[typescript]class  (0) 2018.12.07
[typescript]type assertion  (0) 2018.12.06
[typescript]type  (0) 2018.12.06




*실제로 타입을 바꾸어주는 형변환이 아니고, 컴파일시 그렇게 인지하게 하는 것

*어설션한 타입에 대해서 그 타입이 맞다는 확신이 필요하다, 왜냐하면 컴파일러만 속이는 것이기 때문에 컴파일은 통과해도 코드실행시 에러가 될 수 있기때문












*타입얼라이언스:타입에 별칭을 붙여 부르는 방법만 다르게 하는 것



























유니언 타입:any는 모든 타입이 될 수 있다인데, union타입은 몇 개의 한정적타입만 주어주고 그 타입중 하나라고 선언하는 것




























'frameworks > typescript' 카테고리의 다른 글

[typescript]iterator  (0) 2018.12.11
[typescript]generic(+additional:string literal type)  (0) 2018.12.07
[typescript]class  (0) 2018.12.07
[typescript]interface  (0) 2018.12.06
[typescript]type  (0) 2018.12.06
















































*undefined와 null은 모든 타입의 서프타입이기 때문에, 어떤 타입으로 선언된 변수에도 들어갈 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
class Person{
    name:string = null;
    age: number = null;
    constructor(name:string) {
        this.name = name;
    }
    hello(): void{
        console.log('what the hell');
    }
}
const person = new Person('mark');
console.log(person);
 
cs








































'frameworks > typescript' 카테고리의 다른 글

[typescript]iterator  (0) 2018.12.11
[typescript]generic(+additional:string literal type)  (0) 2018.12.07
[typescript]class  (0) 2018.12.07
[typescript]interface  (0) 2018.12.06
[typescript]type assertion  (0) 2018.12.06

+ Recent posts