Skip to content

Udemy/Ts/section2/22 #27

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 29 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 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
d0975b3
🚩: 타입 할당은 지금까지 하나만 가능했다. 여러 타입을 교차로 사용하면 발생하는 error
4BFC Sep 1, 2024
5dbc685
🚩: error TS 2365 매개변수의 탕
4BFC Sep 1, 2024
0d665a4
🚩: 연산이 허용되지 않는 타입 예시
4BFC Sep 1, 2024
e357dd0
🚩: Union Type으로 발생한 TS 2365 문제를 if문으로 정확한 타입을 명시, 지정 해줌으로써 해결
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
42 changes: 19 additions & 23 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
const person: {
name: string,
age: number,
hobbies: string[];
role: (number | string)[];
} = {
name: 'Maximilian',
age: 30,
hobbies: ['Sports', 'Cooking'],
role: [2, 'author']
};
function combine(n1: number | string, n2: number | string) {
let result;
if (typeof n1 === 'number' && typeof n2 === 'number') {
result = n1 + n2;
} else if (typeof n1 === 'string' && typeof n2 === 'string') {
result = n1 + n2;
} else {
result = n1.toString() + n2.toString();
} // 해결 방법: if문으로 typeof로 각 타입들이 무엇인지 식별한 후 연산을 적용한다. 연산이 적용되지 않는 타입들은 모두 string으로 형변환 후 연산을 시켜주면 된다. 따라서 유니온 타입으로 여러 타입을 유연하게 사용할 수 있지만 이것들을 활용할 때는 정확히 어떤 타입을 받는지에 관한 로직이 보통 함수나 클래스 구조에 많이 필요하다.
return result;
}
// Ts가 염려하는 error의 근원은 아래와 같다.
// const b1: boolean = true;
// const b2: boolean = false;
// console.log(b1 + b2);

person.role.push('admin');
person.role[1] = 10;
person.role = [0, 'admin', 'user'];
const CombineAges = combine(20, 30);
console.log(CombineAges);

//any는 어떤 타입이든 허용하는 타입이다. 어떻게 보면 유연하다고 느껴지질지 모르겠지만 타입스크립트의 장점을 전혀 활용하지 못하기에 좋은 방법은 아니다.
let favoriteActivities: any[];
favoriteActivities = ['Sports', 1]; //이렇게 배열에 마구잡이로 서로 다른 타입을 넣어도 아무런 에러가 일어나지 않는다.

console.log(person.name);

for (const hobby of person.hobbies) {
console.log(hobby);
}
const CombineNames = combine('Max', 'Anna');
console.log(CombineNames);
29 changes: 27 additions & 2 deletions src/basics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function add(n1: number, n2: number, showResult: boolean, phrase: string) {
function addTemplate(n1: number, n2: number, showResult: boolean, phrase: string) {
const result = n1 + n2; //result는 number라고 추론을 한다.
if (showResult) {
console.log(result + phrase);
Expand All @@ -13,4 +13,29 @@ const printResult = true;
const resultPhrase = 'Result is: ';
// resultPhrase = 0; //error -> 타입 추론으로 resultPhrase는 string이다. 따라 0이라는 number타입을 할당할 수 없다.

add(number1, number2, printResult, resultPhrase);
addTemplate(number1, number2, printResult, resultPhrase);

const person: {
name: string,
age: number,
hobbies: string[];
role: (number | string)[];
} = {
name: 'Maximilian',
age: 30,
hobbies: ['Sports', 'Cooking'],
role: [2, 'author']
};

person.role.push('admin');
person.role[1] = 10;
person.role = [0, 'admin', 'user'];

let favoriteActivities: any[];
favoriteActivities = ['Sports', 1];

console.log(person.name);

for (const hobby of person.hobbies) {
console.log(hobby);
}