readonly type
readonly type์ TypeScript์์ ์ฌ์ฉ๋๋ ์ฝ๊ธฐ ์ ์ฉ ํ์ ์ด๋ค.
- ์์ ์ ํด๋ ์ฝ๊ธฐ ์ ์ฉ ํ์ ์ด๊ธฐ ๋๋ฌธ์ ์ค๋ฅ ๋ฐ์์ ํตํด ์์ ํ ์ ์๋ค๋ ์๋ฆผ์ ์ค๋ค.
- ์ฝ๊ธฐ ์ ์ฉ ํ์ ์ ๋ณ์, ํจ์ ๋ฑ ๋ณํ๋ฅผ ์ฃผ๋ฉด ์๋๋ค.
- TypeScipt ์ฝ๊ธฐ ์ ์ฉ ํ์ ์ JavaScript๋ก ์ปดํ์ผ๋์ง ์๋๋ค.
type Age = number;
type Name = string;
type Player = {
readonly name: Name,
age?: Age // ?์ ๋ถ์ด๋ฉด์ number | undefined
}
const playerMaker = (name: string) : Player => ({name})
const imae = playerMaker("Imae")
imae.age = 99
imae.name = "kane" // ์์ ๋ถ๊ฐ! ์ฝ๊ธฐ๋ง ๊ฐ๋ฅ
const numbers: readonly number[] = [1, 2, 3, 4]
numbers.push(1) // error! ์ฝ๊ธฐ ์ ์ฉ
const names: readonly string[] = ["1", "2", "3", "4"]
'๐ค Language > ๐ฆ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TS. Classes (0) | 2022.06.29 |
---|---|
TS. ํจ์ Functions (0) | 2022.06.27 |
TS. Alias (๋ณ์นญ) (2) | 2022.06.20 |
์๋ฐ์คํฌ๋ฆฝํธ ๋์ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ํํ๋ ์ด์ (1) | 2022.06.20 |
TS. ์ธํฐํ์ด์ค interface (0) | 2022.03.22 |