Skip to content

Udemy/Ts/section2/28 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Sep 1, 2024
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d105911
🌱: README을 수정
4BFC Aug 26, 2024
06270b7
🌱: README을 수정
4BFC Aug 26, 2024
5e69dee
🌱: README을 수정
4BFC Aug 29, 2024
d7c1360
🌱: README을 수정
4BFC Aug 29, 2024
978d932
🌱: README을 수정
4BFC Aug 29, 2024
859bffc
🌱: README을 수정
4BFC Aug 29, 2024
e09d95d
🌱: README을 수정
4BFC Aug 29, 2024
3255009
🌱: README을 수정
4BFC Aug 29, 2024
ab9b8db
🌱: README을 수정
4BFC Aug 29, 2024
5f0f9f9
🌱: README을 수정
4BFC Aug 29, 2024
daaeeed
🌱: README을 수정
4BFC Aug 29, 2024
c8ec455
🌱: README을 수정
4BFC Aug 30, 2024
e2a15fd
🌱: README을 수정
4BFC Aug 30, 2024
61dd842
🌱: README을 수정
4BFC Aug 30, 2024
bce5c89
🌱: README을 수정
4BFC Aug 30, 2024
ed8fa00
🌱: issue templates 생성
4BFC Aug 30, 2024
1559fa2
🌱: pr-template 생성
4BFC Aug 30, 2024
691897a
🌱: pr-template 수정
4BFC Aug 30, 2024
44ec035
🌱: pr-template 수정
4BFC Aug 30, 2024
3c3280b
🚩: udemy section2의 14번 강의를 듣고 실습을 했다.
4BFC Aug 31, 2024
cbf04eb
🔀: Merge branch 'UdemyTs'
4BFC Aug 31, 2024
0f07929
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Aug 31, 2024
757fb99
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Aug 31, 2024
7e8ac7d
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Aug 31, 2024
aa89074
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
79304fe
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
eb21a7f
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
cf56f00
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
7dec10d
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
28200e9
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 1, 2024
de29070
🚩: 콜백함수로 구현하기 예시
4BFC Sep 1, 2024
65042f6
🚩: undefined를 반환하는 원인 찾기
4BFC Sep 1, 2024
48b764a
🚩: undefined의 반환 원인 분석 결과
4BFC Sep 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ function printResult(num: number): void {
console.log('Result: ', +num);
}

//콜백함수
function addAndHandle(n1: number, n2: number, cd: (num: number) => void) {
const result = n1 + n2;
//cd(result);
return cd(result); //return으로 반환 값 설정
}

const test = addAndHandle(10, 20, (result) => {
console.log(result);
return result;
});
//콜백함수를 사용하면 어떤 장점이 있는가?
// 함수 안에서 콜백을 전달하면 타입스크립트는 결과가 숫자라는 것을 추론한다. 즉, 해당 함수를 사용할 때 명시적으로 코드를 작성하지 않아도 된다.
console.log(test); //하지만 이렇게 log를 찍어서 확인을 해보면 아무것도 없는 undefined를 반환한다. 왜냐면 반환 타입을 void로 지정했기 때문이다.
//그렇다면 여기서 드는 의문은 addAndHandle의 void때문일까 아니면 cd의 콜백함수에서 암묵적으로 지정한 void때문일까? 이유는 리터럴 타입을 확인하면 된다. addAndHandle의 반환되는 리터럴 타입은 number이다. 타입을 지정하지 않았기 때문에 암묵적으로 result의 타입을 끌고와서 number로 타입이 지정된다. 따라서 함수 타입을 지정하거나 return값을 지정해주면 undefined가 아닌 정상적인 코드가 출력 될 것이다.

printResult(add(1, 2));

// let result: Function; //일반적인 방식으로 함수 타입을 할당한 경우
let result: (a: number, b: number) => number; // 화살표기로 함수의 반환 값까지 할당한 경우
let result: (a: number, b: number) => number;
result = add;
//result = printResult; // error ts(2322) -> 'void' 형식은 'number' 형식에 할당할 수 없습니다.
// 해당 오류는 result의 타입은 number로 반환되는 함수여야만 가능하기 때문이다. 즉, printResult는 void가 반환이기 때문에 오류가 발생한다.
// result = 0; //값이 재할당 되면서 0으로 값이 출력된다.
console.log(result(1, 2)); // 그렇다면 result 변수에 함수 타입을 지정하면 되지 않을까?
console.log(result(1, 2));