타입스크립트 readonly

Jaejun Kim·2023년 1월 10일
0

타입스크립트

목록 보기
4/9
// read only === 읽기만 가능 === 수정 불가능 

type ReadOnly = {
  readonly name: string
}

// 즉 객체 내용물 바꾸는거 안됨
const readonly_string: ReadOnly = {
  name: "jaejun"
}
readonly_string.name = "jaemin"
// Cannot assign to 'name' because it is a read-only property.

// 배열 push도 못하게 막을 수 있음
const readonly_number_arr: readonly number[] = [1, 2, 3, 4]
readonly_number_arr.push(1)
// Property 'push' does not exist on type 'readonly number[]'

const readonly_string_arr: readonly string[] = ['1', '2', '3', "4"]
readonly_string_arr.push('1')
// Property 'push' does not exist on type 'readonly number[]'
// 기본자료형도 당연히 되겠지. 근데 이건 const면 충분하잖아

배움출처: https://nomadcoders.co/typescript-for-beginners/lobby

0개의 댓글