Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a195ae2
commit ecbd232
Showing
1 changed file
with
20 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,44 @@ | ||
// 타입 추론 없이 타입 선언하기 | ||
// 타입 추론을 활용하여 타입 선언하기 | ||
|
||
// 변수에 타입 선언 | ||
|
||
let a: number; // 선언만 | ||
let b: string = "Hello"; | ||
let a: number; // 타입 추론이 불가하므로 타입 선언 필요 | ||
// let a; // 선언만 할 때 아무 타입도 지정하지 않으면 "any" 타입으로 선언됩니다.(권장 안함) | ||
let b = "Hello"; // 할당(초기화)된 값의 타입으로 타입 추론(string) | ||
a = 12; // Good | ||
// a = "Bye"; // Error : 'string' 형식은 'number' 형식에 할당할 수 없습니다. | ||
b = "Bye"; // Good | ||
// b = 12; // Error : 'number' 형식은 'string' 형식에 할당할 수 없습니다. | ||
|
||
const c: string = "Hi"; | ||
const arr1: number[] = [12, 35]; | ||
const arr2: [number, boolean] = [12, true]; | ||
const arr3: (number | boolean)[] = [12, true]; | ||
const c = "Hi"; // 할당(초기화)된 값의 타입으로 타입 추론(string) | ||
const arr1 = [12, 35]; // 숫자 타입으로만 이루어진 배열로 타입 추론 | ||
|
||
const obj1: { | ||
key1: boolean; | ||
key2: string; | ||
key3: number; | ||
} = { | ||
// 목적에 따라 타입을 선언해줘야 하는 경우 | ||
const arr2: [number, boolean] = [12, true]; // 타입 선언 필요 : 선언하지 않으면 (number | boolean)[] 유니온 타입으로 추론됨 | ||
const arr3 = [12, true]; // 타입 선언 불필요 : 원하는 타입으로 추론 | ||
|
||
const obj1 = { | ||
key1: true, | ||
key2: "Hi", | ||
key3: 123, | ||
}; | ||
}; // 각각 프로퍼티의 값의 타입으로 타입 추론 | ||
|
||
// 함수 선언할 때 타입 선언 | ||
|
||
function fun1(a: number, b: number): string { | ||
const sum: number = a + b; | ||
function fun1(a: number, b: number) { | ||
// return 타입 선언 불필요 : return 타입이 코드 문맥상 확실히 string | ||
const sum = a + b; // 타입 선언 불필요 : 타입이 코드 문맥상 확실히 number | ||
return sum.toString(); | ||
} | ||
|
||
const fun2 = (a: number, b: number): string => { | ||
const sum: number = a + b; | ||
const fun2 = (a: number, b: number) => { | ||
// return 타입 선언 불필요 : return 타입이 코드 문맥상 확실히 string | ||
const sum = a + b; // 타입 선언 불필요 : 타입이 코드 문맥상 확실히 number | ||
return sum.toString(); | ||
}; | ||
|
||
const fun3 = ( | ||
a: number, | ||
b: number, | ||
c: (d: number, e: number) => string | ||
): string => { | ||
const fun3 = (a: number, b: number, c: (d: number, e: number) => string) => { | ||
// return 타입 선언 불필요 : return 타입이 코드 문맥상 확실히 string | ||
return c(a, b); | ||
}; | ||
console.log(fun3(1, 3, fun2)); |