Skip to content

Udemy/Ts/section2/16 #20

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 24 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 6 additions & 13 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
function add(n1, n2, showResult, phrase) {
if (showResult) {
console.log(n1 + n2);
}
else {
return n1 + n2;
}
}
var number1 = 5;
var number2 = 2.8;
var printResult = true;
var resultPhrase = 'Result is: ';
add(number1, number2, printResult, resultPhrase);
//console.log(person.name); 으로 person.name, 객체 타입으로 접근하기 위한 object 타입 정의 방법
var person = {
name: 'Maximilian',
age: 30
};
console.log(person.name); //error : Object에 name이 없습니다. 그렇다면 어떻게 해야할까?
27 changes: 12 additions & 15 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
function add(n1: number, n2: number, showResult: boolean, phrase: string) {
const result = n1 + n2; //result는 number라고 추론을 한다.
if (showResult) {
console.log(result + phrase);
} else {
return n1 + n2;
}
}
//console.log(person.name); 으로 person.name, 객체 타입으로 접근하기 위한 object 타입 정의 방법
const person: {
//entry 추가
//앞서 말했 듯 객체 타입을 정의하기 위해서는 할당 후 `;`를 붙여야 한다.
name: string;
age: number;
// 이렇게 작성을 하게 되면 추후에 정의한 객체 타입을 재활용할 수 있게 된다.
} = { //`{}` 특정 객체 타입을 위한 표기법, 객체의 구조 정보를 제공하는 것. 빈 `{}`으로 할당을 하면 객체로 할당하는 것과 동일시한다.
name: 'Maximilian',
age: 30
};

const number1 = 5;
const number2 = 2.8;
const printResult = true;
const resultPhrase = 'Result is: ';
// resultPhrase = 0; //error -> 타입 추론으로 resultPhrase는 string이다. 따라 0이라는 number타입을 할당할 수 없다.

add(number1, number2, printResult, resultPhrase);
console.log(person.name); //error : Object에 name이 없습니다. 그렇다면 어떻게 해야할까?
16 changes: 16 additions & 0 deletions src/basics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function add(n1: number, n2: number, showResult: boolean, phrase: string) {
const result = n1 + n2; //result는 number라고 추론을 한다.
if (showResult) {
console.log(result + phrase);
} else {
return n1 + n2;
}
}

const number1 = 5;
const number2 = 2.8;
const printResult = true;
const resultPhrase = 'Result is: ';
// resultPhrase = 0; //error -> 타입 추론으로 resultPhrase는 string이다. 따라 0이라는 number타입을 할당할 수 없다.

add(number1, number2, printResult, resultPhrase);