Tech Blog of Pinomaker
[TypeScript] any, unknown, void, never
F.E/TypeScript 2022. 8. 2. 23:46

any any는 TypeScript의 보호장치에서 벗어나고 싶다면 사용하는 자료형이다. 아무 타입이나 될 수 있다. const a : any[] = [1,2,3,4,5] const b : any = true console.log(a+b) // Success "1,2,3,4,5true" unknown unknown은 API에서 응답을 받을 때 등 자료가 어떤 타입이 올 지 모를 때 사용하며, 타입을 먼저 확인해야 사용할 수 있다. let a : unknown const b = a + 1 // Error -> b의 타입이 unknown이라 타입을 확인 할 수 없어 에러 발생 if(typeof a === "number"){ const c = a + 1 // if문에서 타입이 number임을 확인하여 성공 } ..

[TypeScript] ReadOnly : 수정 불가능한 데이터
F.E/TypeScript 2022. 8. 2. 23:27

readonly readonly는 타입 스크립트에서 사용하는 것으로, 선언을 하면 데이터의 수정이 불가능하다. 객체에서의 readonly const person : { readonly name : string, age : number} = { name : "피노", age : 22 } person.age = 23 person.name = "피노2" // Error -> readonly이기에, 수정 불가능 배열에서의 readonly const numbers : readonly number[] = [1,2,3,4] numbers.push(5) // Error -> 배열은 readonly로 수정 불가능

[TypeScript] TypeScript 시작하기 - 자료형
F.E/TypeScript 2022. 8. 2. 23:18

1. 타입 스크립트 타입 스크립트는 변수 뒤에 타입을 지정 할 수 있다. 변수 타입에 맞지 않는 값을 넣으면 에러 발생 const a : string = "Hello World" const b : string = 123 // ERROR! 타입 스크립트의 타입 시스템 number : 숫자 자료형 string : 문자열 자료형 boolean : 참, 거짓 자료형 배열, 객체, 함수 ... const a : string = "Hello world" const b : number = 123 const c : boolean = true const d : string[] = ["a", "b"] const e : {name : string, age : number} = { name : "피노키오", age : 22 ..