ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [typescript] interface , assertion , generic+allias
    typescript 2021. 6. 17. 15:14
    반응형

    type alias + union type + generic

     

    type Pair<T> = [T , T]
    
    let marry2 : Pair<string> = ["tmddlr","tksk"]

    인터페이스에는 객체도 되고 , 함수도되고 , 클래스 도 정해 줄수 있다.

    interface Printable {
    	read():void
    }
    
    interface Printable {
    	read();
    }     >>>> 'read', which lacks return-type annotation, implicitly has an 'any' return type

    타입 단언 (type assertion)

    const myInp = document.getElementById("my")
    console.log(myInp.value)
    
    >> myInp 에 value 가 있다라는 보장이 없다.
    
    const myInp : HTMLInputElement = document.getElementById("my")
    console.log(myInp.value)
    
    >> document.getElementById("my") 가 inputElement 라는 보장이 없다.
    
    const myInp : HTMLInputElement = document.getElementById("my") as HTMLInputElement
    console.log(myInp.value)
    
    >> 타입단언은 코드작성자가 더 잘 알고 있다라는 식의 코드이다. 때로는 이점이 위험할수 있다.
    
    또는 
    
    const myInp : HTMLInputElement = <HTMLInputElement>document.getElementById("my")
    console.log(myInp.value)

    'typescript' 카테고리의 다른 글

    [typescript]나중에봐야지  (0) 2022.12.08
    [typescript] 연습장(예시)  (0) 2021.06.15
    [타입스크립트] typeguard  (0) 2021.06.15
    [타입스크립트] 연습장  (0) 2021.06.11
    [타입스크립트] enum ??  (0) 2021.06.04
Designed by Tistory.