Skip to content

Udemy/Ts/section2/27 #31

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
367bda7
🚩: 함수를 변수로 할당 했을 때 발생하는 예외사황 얘시
4BFC Sep 1, 2024
2be21e5
🚩: 함수로 타입을 할당한 변수를 활용한 예시
4BFC Sep 1, 2024
aecf2f3
🚩: undefined 출력되는 void 함수 예시
4BFC Sep 1, 2024
3f3344d
🚩: 반환 타입을 변수에 직접 지정할 수 있는 방식 예시
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
33 changes: 13 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
function add(n1: number, n2: number): number {
return n1 + n2; //error ts(2322) 'number' 형식은 'string' 형식에 할당할 수 없습니다.
} //현재 return에 정의한 n1+n2로 인해서 add함수의 반환 타입은 Number이다. 만약 toSting으로 형변환을 한 `n1.toString() + n2.toString()`일 경우 반환된 값은 string으로 타입이 반환 될 것이다.

function printResult(num: number | string | undefined): void {
if (typeof num === 'number' || typeof num === 'string') {
console.log('Result: ', +num);
} else {
console.log('this is Undefined!');
}
} //일반적으로 함수의 반환 타입을 정하지 않으면 void로 판단한다. `:void`를 명시하지 않아도 해당 함수에 마우스를 호버하면 void로 판별 되어 있는 것을 확인 할 수 있다. 따라서 굳이 void를 명시할 필요는 없다.

//반환되지 않는 함수를 기준으로 log로 확인을 해보면 어떤 결과가 나올까
console.log(printResult(add(5, 12))); //결과적으로 `undefined`라는 결과가 나온다. 반환된 값이 없기 때문에 정의되지 않은 박스만 출력이 되는 것이다. 참고로 `undefined`는 값이 없는게 아니라 값을 담아내고 있는 박스에 아무것도 담겨 있지 않다는 의미의 `값`이다. 즉, 비어 있는 박스를 출력한 것이다.

let UndefinedValue: undefined; //변수로 undefined라는 타입을 지정할 수는 있다. 하지만 함수에서는 undefined라는 타입을 할당 하면 안된다. 반환값이 없는 함수를 출력하면 undefined가 출력되는데 이는 함수의 활용과 의미가 상실하게 된다.
return n1 + n2;
}

function UndefinedFunction(value: string): undefined {
console.log(value);
// return value; //error ts(2322) -> 'string' 형식은 'undefined' 형식에 할당할 수 없습니다
return; //결과적으로 함수에 아무것도 없는 return이여야 undefined가 된다.
function printResult(num: number): void {
console.log('Result: ', +num);
}

printResult(UndefinedFunction('test'));
printResult(add(1, 2));

// let result: Function; //일반적인 방식으로 함수 타입을 할당한 경우
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 변수에 함수 타입을 지정하면 되지 않을까?