Tuple
Tuple์ array๋ฅผ ์์ฑํ ์ ์๊ฒ ํ๋ค.
const players: readonly [string, number, boolean] = ["imae", 99, true]
players[0] = "hi" // error! readonly!
let a: undefined = undefined
let b: null = null
๋ฐฐ์ด์ธ๋ฐ ํ์ ์ด ํ๊ฐ์ง๊ฐ ์๋ ๊ฒฝ์ฐ์ด๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ์ฒด์ด๊ณ ๊บผ๋ด ์ฌ์ฉํ ๋, ์ฃผ์๊ฐ ํ์ํ๋ค.
๋ฐฐ์ด์ Destrurctutingํ๋ฉด ํ์ ์ด ์ ๋๋ก ์ป์ด์ง๋ค.
let x: [string, number];
x = ["hello", 39]; // ์์, ํ์
, ๊ธธ์ด๋ ๋ง์์ผํ๋ค.
x = [10, "Imae"]; // ์์๊ฐ ๋ฐ๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
x[3] = "world"; // ์๋ฌ๊ฐ ๋ฐ์ ts(2322)
const person: [string, number] = ["Imae", 32];
const[first, second, third] = person; // first๋ string, second๋ number, third๋ ์ค๋ฅ ๋ฐ์
any
๊ธฐ๋ณธ์ ์ผ๋ก any๋ ๋น์ด์๋ ๊ฐ์ ์ฌ์ฉํ ๋ ์ฌ์ฉํ๋ค.
Typescript๋ก๋ถํฐ ๋น ์ ธ๋์ค๊ณ ์ถ์ผ๋ฉด ์ฐ๋ ํ์ ์ฆ, any๋ JavaScript์ฒ๋ผ ์๋ฌด ํ์ ์ด๋ ๋ ์ ์๋ค.
any ์ฌ์ฉ์ ์์ฃผ ํ๋ ๊ฒ์ ์ข์ง ์๋ค. ์๋ํ๋ฉด TypeScript์ ์ฅ์ ์ธ TypeScript์ ๋ณดํธ์ฅ์น๊ฐ ์์ด์ง๋ค.
const c: any[] = [1, 2, 3, 4]
const d: any = true
c + d
์ด๋ค ํ์ ์ด์ด๋ ์๊ด์๋ ํ์ ์ด๋ค. ์ด๊ฑธ ์ต๋ํ ์ฐ์ง ์๋๊ฒ ํต์ฌ์ด๋ค. ์๋ํ๋ฉด ์ปดํ์ผ ํ์์ ํ์ ์ฒดํฌ๊ฐ ์ ์์ ์ผ๋ก ์ด๋ค์ง์ง ์๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋์ ์ปดํ์ผ ์ต์ ์ค์๋ any๋ฅผ ์จ์ผํ๋๋ฐ ์ฐ์ง ์์ผ๋ฉด ์ค๋ฅ๋ฅผ ๋ฑ๋๋ก ํ๋ ์ต์ ์ธ nolmplicitAny๋ ์๋ค.
function returnAny(message: any): any {
console.log(message);
}
const any1 = returnAny("๋ฆฌํด์ ์๋ฌด๊ฑฐ๋");
any
- any๋ ๊ณ์ํด์ ๊ฐ์ฒด๋ฅผ ํตํด ์ ํ๋๋ค.
- ๊ฒฐ๊ตญ, ๋ชจ๋ ํธ์๋ ํ์ ์์ ์ฑ์ ์๋ ๋๊ฐ๋ก ์จ๋ค.
- ํ์ ์์ ์ฑ์ TypeScript๋ฅผ ์ฌ์ฉํ๋ ์ฃผ์ ๋๊ธฐ ์ค ํ๋์ด๋ฉฐ ํ์ํ์ง ์์ ๊ฒฝ์ฐ์๋ any๋ฅผ ์ฌ์ฉํ์ง ์๋๋ก ํด์ผํ๋ค.
any1.toString();
let looselyTyped: any = {};
const d = looselyTyped.a.b.c.d; // const d: any
Avoid leaking any
any๋ ์กด์ฌํ ์ ๋ฐ์ ์๋ค. any๋ก ์ง์ ๋ ๊ฐ์ ์ด์ฉํด์ ์๋ก์ด ๊ฐ์ผ๋ก ๋ง๋ค์ด ์ฌ์ฉํ๋ ์ง์ ์์ ๋์๋ฅผ ๋ง์์ฃผ๋๋ก ๋ ธ๋ ฅ์ด ํ์ํ๋ค.
function leakingAny(obj: any) {
const a = obj.num; // a๋ฅผ number๋ก ๊ท์ ํ๋ฉด b๋ number, c๋ ๋๋ฒ
const b = a + 1;
return b;
}
const c = leakingAny({num: 0}); // const c = any
c.indexOf("0") // a๋ฅผ number๋ก ๊ท์ ํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
unknown
- unknown์ JavaScript์๋ ์กด์ฌํ์ง ์๊ณ TypeScript์๋ง ์กด์ฌํ๋ ๊ฒ์ด๋ค.
- ๋ง์ฝ API๋ก๋ถํฐ ๋ณ์๋ฅผ ๋ฐ๋๋ฐ ์ด๋ค ํ์ ์ธ์ง ๋ชจ๋ฅธ๋ค๋ฉด unknown์ ์ฌ์ฉํ๋ค.
- unknown์ ์ฌ์ฉํ๊ธฐ ์ ์ type of๋ฅผ ์์๋ณด์์ผ ํ๋ค.
let a:unknown;
let b = a + 1 // a๊ฐ number์ธ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ error ๋ฐ์
if(typeof a=== 'number'){
let b = a + 1
}
iif(typeof a === "string"){
let b = a.toUpperCase();
}
- ์์ฉ ํ๋ก๊ทธ๋จ์ ์์ฑํ ๋ ๋ชจ๋ฅด๋ ๋ณ์์ ํ์ ์ ๋ฌ์ฌํด์ผ ํ ์๋ ์๋ค.
- ์ด๋ฌํ ๊ฐ์ ๋์ ์ฝํ ์ธ (์: ์ฌ์ฉ์๋ก๋ถํฐ, ๋๋ ์ฐ๋ฆฌ API์ ๋ชจ๋ ๊ฐ์ ์๋์ ์ผ๋ก ์๋ฝํ๊ธฐ๋ฅผ ์ํ ์ ์๋ค.)
- ์ด ๊ฒฝ์ฐ, ์ปดํ์ผ๋ฌ์ ๋ฏธ๋์ ์ฝ๋๋ฅผ ์ฝ๋ ์ฌ๋์๊ฒ ์ด ๋ณ์๊ฐ ๋ฌด์์ด๋ ๋ ์ ์์์ ์๋ ค์ฃผ๋ ํ์ ์ ์ ๊ณตํ๊ธฐ๋ฅผ ์ํ๋ฏ๋ก unknownํ์ ์ ์ ๊ณตํ๋ค.
typeof ๊ฒ์ฌ, ๋น๊ต ๊ฒ์ฌ ๋๋ ๊ณ ๊ธ ํ์ ๊ฐ๋๋ฅผ ์ํํ์ฌ ๋ณด๋ค ๊ตฌ์ฒด์ ์ธ ๋ณ์๋ก ์ขํ ์ ์๋ค.
declare const maybe: unknown;
const aNumber: number = maybe; // unknown์ number์ ๋ฐ๋ก ํ ๋นํ ์ ์๋ค.
if (maybe === true) {
const aBoolean: boolean = maybe;
const aString: string = maybe; // string์ ์ ์ธํ ์ ์๋ค.
}
if (typeof maybe === 'string') {
const aStirng: string = maybe;
const aBoolean: boolean = maybe; // boolean์ผ๋ก ์ ์ธํ ์ ์๋ค.
}
- TypeScript 3.0 ๋ฒ์ ๋ถํฐ ์ง์ํ๋ค.
- any์ ์ง์ผ๋ก any๋ณด๋ค Type-safeํ ํ์
์ด๋ค.
- any์ ๊ฐ์ด ์๋ฌด๊ฑฐ๋ ํ ๋นํ ์ ์๋ค.
- ์ปดํ์ผ๋ฌ๊ฐ ํ์ ์ ์ถ๋ก ํ ์ ์๊ฒ๋ ํ์ ์ ์ ํ์ ์ขํ๊ฑฐ๋ ํ์ ์ ํ์ ํด์ฃผ์ง ์์ผ๋ฉด ๋ค๋ฅธ ๊ณณ์ ํ ๋น ํ ์ ์๊ณ , ์ฌ์ฉํ ์ ์๋ค.
- unknown ํ์
์ ์ฌ์ฉํ๋ฉด runtime error๋ฅผ ์ค์ผ ์ ์์ ๊ฒ ๊ฐ๋ค.
- ์ฌ์ฉ ์ ์ ๋ฐ์ดํฐ์ ์ผ๋ถ ์ ํ์ ๊ฒ์ฌ๋ฅผ ์ํํด์ผ ํจ์ ์๋ฆฌ๋ API์ ์ฌ์ฉํ ์ ์์ ๊ฒ ๊ฐ๋ค.
never
never์ JavaScript์๋ ์กด์ฌํ์ง ์๊ณ TypeScript์๋ง ์กด์ฌํ๋ ๊ฒ์ด๋ค.
never์ ํจ์๊ฐ ์ ๋ returnํ์ง ์์ ๋ ๋ฐ์ํ๋ค. ์ฆ, ํจ์์์ exception(์์ธ)์ด ๋ฐ์ํ ๋ ์ฌ์ฉํ๋ค.
function hello(name:string|number):never{
if(typeof name === "string"){
name
} else if (typeof name === "number") {
name
} else {
name
}
}
๋ฆฌํด์ ์ฌ์ฉ๋๋ค. ๋ฆฌํด์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ, ์๋ 3๊ฐ์ง ์ ๋๊ฐ ๋๋ถ๋ถ์ด๋ค.
function error(message: string): never {
throw new Error(message);
}
function fail() {
return error("failed");
}
function infiniteLoop(): never {
while(true) {}
}
never ํ์ ์ ๋ชจ๋ ํ์ ์ subtype์ด๋ฉฐ, ๋ชจ๋ ํ์ ์ ํ ๋นํ ์ ์๋ค. ํ์ง๋ง, never์๋ ๊ทธ ์ด๋ค ๊ฒ๋ ํ ๋นํ ์ ์๋ค.
any ์กฐ์ฐจ๋ never์๊ฒ ํ ๋นํ ์ ์๋ค. ์๋ชป๋ ํ์ ์ ๋ฃ๋ ์ค์๋ฅผ ๋ง๊ณ ์ ํ ๋ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
declare const a: string | number;
if (typeof a !== 'string') {
a; // const a: number
}
type Indexable<T> = T extends string ? & {[index: string]: any} : never;
type ObjectIndexable = Indexable<{}>; // ์๋ชป๋ ํ์
์ ๋ฃ๋ ์ค์๋ฅผ ๋ง๋ ์ฉ๋๋ก ์ฌ์ฉํ๋ค.
const b: Indexable<{}> = ''; // ์๋ชป๋ ํ์
์ ๋ฃ์ด์ ์ค๋ฅ ๋ฐ์
void
void๋ JavaScript์๋ ์กด์ฌํ์ง ์๊ณ TypeScript์๋ง ์กด์ฌํ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ค ํ์ ๋ ๊ฐ์ง์ง ์๋ ๋น ์ํ๋ฅผ ์๋ฏธํ๋ค.
์ฆ, ์๋ฌด๊ฒ๋ ๋ฆฌํดํ์ง ์๋ ํจ์๋ค.
function hello(){
console.log('x')
}
const a = hello(); // void๊ฐ
a.toUpperCase() // error!
๊ฐ์ ์๊ณ ํ์ ๋ง ์๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฌธ์๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ ๋ฐํํ์ง ์๋ ํจ์์ ๋ฆฌํด ํ์ ์ผ๋ก ์ฌ์ฉํ๋ค. ๊ทธ์ธ์๋ ์ฌ์ฉํ ์ผ์ด ๊ฑฐ์ ์๋ค.
ํ ๋น์ด ๊ฐ๋ฅํ ๊ฐ์ undefined์ด๋ค.
function returnVoid(message: string): void {
// function returnVoid(message: string): void
console.log(message);
return undefined; // undefine๋ง ์ ์ผํ๊ฒ void์ ํ ๋นํ ์ ์๋ ํํ์ด๋ค.
}
const r = returnVoid("๋ฆฌํด์ด ์๋ค."); // const r: void
'๐ค Language > ๐ฆ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TS. Alias (๋ณ์นญ) (2) | 2022.06.20 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ ๋์ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ํํ๋ ์ด์ (1) | 2022.06.20 |
TS. ์ธํฐํ์ด์ค interface (0) | 2022.03.22 |
TS. ํ์ ์์คํ Type System (0) | 2022.03.16 |
TS. Type / Boolean / Number / String / Symbol / Null & undefined / Object / Array (0) | 2022.03.13 |