✊ 필오의 개발일지
Back to Posts
2017년 11월 17일

3/ Typescript_기본 데이터 타입

3/ Typescript_기본 데이터 타입

기본 데이터 타입 🌱

타입스크립트에서 기본 자료형을 잘! 숙지해야 한다. 타입스크립트는 타입을 새로 만들면서 짜는 방식인데, 특정한 골격을 갖출 때 결국 남는건 기본 자료형들이 남는다. 이때 기본 자료형이 정확히 어떤 것들이 있는지 인지하고 있어야, 그 기본 자료형들을 잘 조합해서 내가 사용할 인터페이스를 만들어 낼 수 있고, 타입을 만들 수 있다. 자바스크립트의 기본자료형을 모두 포함한다.


타입스크립트 정리 글은 이웅재님의 강의 강의록 을 참고하여 작성하였습니다. (짱짱) 오류가 있다면 언제든지 댓글 부탁드립니다 .

0. literal


1. 기본 자료형 Primitive Type

let name = 'Nayoung'; name.toString();

1-1. boolean

소문자 boolean 과 대문자 Boolean

let isDone: boolean = false typeof isDone === 'boolean' // true // Type 'boolean' is assignable to type 'Boolean'. let isOk: Boolean = true // Type 'Boolean' is not assignable to type 'boolean'. // 'boolean' is a primitive, but 'Boolean' is a wrapper object. // Prefer using 'boolean' when possible. let isNotOk: boolean = new Boolean(true)

1-2. number

let decimal: number = 6 // 10진수 리터럴 let hex: number = 0xf00d // 16진수 리터럴 let binary: number = 0b1010 // 2진수 리터럴 let octal: number = 0o744 // 8진수 리터럴

1-3. string

let name: string = 'mark' name = 'anna'

1-4. Template String

let fullName: string = `Bob Bobbington` let age: number = 37 let sentence: string = `Hello, my name is ${fullName}. I'll be ${age + 1} years old next month.` // template string 을 사용하지 않을 경우 let sentence: string = 'Hello, my name is ' + fullName + '.\n\n' + "I'll be " + (age + 1) + ' years old next month.'

1-5. Undefined & null

// 이 변수들에 할당할 수 있는 것들은 거의 없다. let u: undefined = undefined let n: null = null

1-5-1. undefined & null are subtypes of all other types

// 대입 가능 let name: string = null let age: number = undefined // strictNullChecks => true // 보통 위의 옵션을 true로 하진 않는다.. // 위의 옵션이 추가되면 자기 자신이나 void에게만 할당 가능, 다른 타입에는 할당하지 못함. // let name: null = null; // (O) // Type 'null' is not assignable to type 'string'. let name: string = null // (X) // null => null || void, undefined => undefined || void // Type 'null' is not assignable to type 'undefined'. let u: undefined = null // (X) let v: void = undefined // (O) let union: string | null | undefined = 'str'

1-5-2. null in JavaScript

let n: null = null console.log(n) // null console.log(typeof n) // object

1-5-3. undefined in JavaScript

let u: undefined = undefined console.log(u) // undefined console.log(typeof u) // undefined

2. 참조타입 (javaScript)

2-1. Array

let list: number[] = [1, 2, 3] let list: Array<number> = [1, 2, 3]

2-2. Symbol 🙄

let sym = Symbol(); let obj = { [sym]: 'value', }; console.log(obj[sym]); // "value"

3. 타입스크립트에서 추가로 제공하는 타입

3-1. Void

function returnVoid(message): void { console.log(message) } returnVoid('리턴이 없다')

3-2. Any

function returnAny(message): any { console.log(message) } returnVoid('리턴은 아무거나')

3-3. Never

// Function returning never must have unreachable end point function error(message: string): never { throw new Error(message) } // Inferred return type is never function fail() { return error('Something failed') } // Function returning never must have unreachable end point function infiniteLoop(): never { while (true) {} }

3-4. Tuple

// Declare a tuple type let x: [string, number] // Initialize it x = ['hello', 10] // OK // Initialize it incorrectly x = [10, 'hello'] // Error x[3] = 'world' // OK, 'string' can be assigned to 'string | number' console.log(x[5].toString()) // OK, 'string' and 'number' both have 'toString' x[6] = true // Error, 'boolean' isn't 'string | number' const person: [string, number] = ['mark', 35] const [name, age] = person

3-5. Enum

enum Color {Red, Green, Blue} let c: Color = Color.Green; enum Color {Red = 1, Green, Blue} let c: Color = Color.Green; enum Color {Red = 1, Green = 2, Blue = 4} let c: Color = Color.Green; enum Color {Red = 1, Green, Blue} let colorName: string = Color[2]; // 결과값은 string으로 해야한다.

참고링크

  1. http://jaroinside.tistory.com/10 
  2. http://poiemaweb.com/typeScript-vscode 
  3. http://spectrumdig.blogspot.kr/2016/12/chrome-source-map-coffeescripttypescrip.html 
Previous4/ 타입추론, Type assertions, Type alias
Next2016년 A월 B일

Related

© 2025 Felix