Skip to content
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
621e700
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 2, 2024
5f9833d
Merge branch 'UdemyTs' of https://github.com/Programming-Contents-Lis…
4BFC Sep 2, 2024
78fbad9
🚩: never 타입과 throw함수의 연관성 그리고 사용 방법 예시
4BFC Sep 2, 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
18 changes: 10 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
let userInput: unknown; // any 와 비교가 되는 unknown Type
//unknown Type은 어떤 타입이 사용될지 모를 때 사용된다.
//any와의 차이
// userName에 userInput을 할당 했지만 오류가 발생한다. unknown을 any로 교체하면 해당 에러는 사라진다. 즉, 타입이 정확히 정해지지 않았을 뿐, any처럼 어떤 값이든 무작위로 사용을 허용한다는 것은 아니다. 다시 말해서 typeof로 확인을 해보면 마지막에 할당 한 값으로 userInput은 값이 할당 되어 있다.literal Type을 확인하면 여전히 unknown으로 할당이 된다.
let userInput: unknown;

let userName: string;
userInput = 5;
userInput = 'Max';

console.log(typeof userInput);

//userName = userInput; //error ts(2322) -> 'unknown' 형식은 'string' 형식에 할당할 수 없습니다.
//그렇다면 userName에 userInput 값을 할당이 가능하게 하려면 어떻게 해야할까? 지난 강의에서도 계속 언급했듯 if문이라는 조건문을 통해서 userName의 타입을 확인 할 수 있는 환경을 조성해주면 된다.

if (typeof userInput === 'string') {
userName = userInput;
}

//결론적으로 any보다 unknown으로 타입을 할당하게 되면 if문으로 검수,검토를 통해서 값을 재할당하거나 활용할 수 있기 때문에 가시적으로도 코드의 가독성적으로도 훨씬 안정적인 코드를 작성할 수 있다.
function generateError(message: string, code: number): never { //:never
throw { message: message, errorCode: code };
} //generateError는 never타입을 반환한다. 이유는 throw 때문인데 절대적으로 값이 변하면 안되기 때문이다. 따라서 never타입은 다음과 같다.
//never 타입은 어떠한 값도 반환하지 않는 함수의 반환 타입을 나타낸다. 이 타입은 함수가 정상적으로 완료되지 않고 항상 예외를 던지거나 무한 루프에 빠져 끝나지 않는 경우에 사용된다. 오해하지 말아야할 것은 literal type으로 확인을 하면 void로 선언이 되어있지만 throw를 사용하면 무조건으로 never가 반환된다. 따라서 암묵적으로 never를 반환은 하지만 never타입임을 코드에 명시해주는 것이 좋다.

const resultError = generateError('An error occurred!', 500);
console.log('resultError: ', resultError); //본래 일반적인 함수라면 undefined를 반환해야한다.
//하지만 아무것도 반환되는 것이 없다. 이유는 generateError는 never 타입으로 반환이 되기 때문에 컴파일이 중도 정지가 되는 것이다.
//참고로 throw는 try,catch를 사용해도 무시하지 않고 중도 정지가 된다.