Typescript [2] - 타입추론,타입명

lionloopy·2023년 5월 6일
0

타입스크립트

목록 보기
2/8

타입추론

: 타입 표기가 없는 경우, 타입스크립트는 우리의 코드를 읽고 분석하여 타입을 유추해낼 수 있다.
#1

let a = 5
a = "hello"

=> 불가능

let a = 5

a 는 자동으로 숫자 number 타입이 지정됨. a:number

#2

let student = {
  name: "Jake", //string
  course: "Getting started with typescript",  //string
  codingIQ: 80,  //number
  code: function () {  //void
    console.log("brain is working hard");
  },
};

#3

function calculate(lostPoints) {
  return 100 - lostPoints;
}

이렇게 코드를 작성시, calculate 함수는 알아서 type이 any:number로 유추된다.

타입명시

: 변수를 선언할 때, 변수 값의 타입을 명시함으로써 변수 값의 데이터 타입을 지정한다.

let a : string = '나는 영원한 문자열!'
function getTitle (ID:number) : object {}

함수또한 타입을 명시해줄 수 있고, 여기서 void는 반환해주는 값이 없을 때 지정할 수 있다.

profile
Developer ʕ ·ᴥ·ʔ ʕ·ᴥ· ʔ

0개의 댓글