1) Type Declare

TypeScript 에는 JavaScript 와 달리 변수에 타입을 지정할 수 있습니다.

const title: string = "Jasmine";

타입 지정 방법은 변수명 뒤에 콜론 : 을 붙이고 타입명을 지정해주면 됩니다.

TypeScript Type List

타입 사용 예제

1. String

const title: string = "Jasmine"
console.log(title);  // Jamine

/* error */
const strErr1:string = 123 // Type '123' is not assignable to type 'string'.
const strErr2:string = true // Type 'true' is not assignable to type 'string'.

<aside> 💡 stringString 의 차이

</aside>

TypeScript 에서 제공하는 string 타입과 JavaScript 에 있는 String 타입은 다른 타입입니다.

String 타입의 경우, 저희가 일반적으로 사용하는 문자열과 String 타입의 객체를 모두 포함합니다.

반대로 string 타입은 new 메소드를 통해 생성한 String 타입의 객체와는 다른 타입입니다.