Skip to content

Commit f745d48

Browse files
authored
๐Ÿ”€: Udemy/Ts/section2/23 (#28)
* ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: issue templates ์ƒ์„ฑ Github์—์„œ ์ž‘์„ฑํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: pr-template ์ƒ์„ฑ Github์—์„œ ์ž‘์„ฑํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: pr-template ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: pr-template ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐Ÿšฉ: udemy section2์˜ 14๋ฒˆ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์‹ค์Šต์„ ํ–ˆ๋‹ค. * ๐Ÿšฉ: ์ผ๋ฐ˜์ ์ธ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์— ๋”ฐ๋ฅธ ์กฐ๊ฑด๋ฌธ ๊ฒฐ๊ณผ * ๐Ÿ› : error ts2365 ์—๋Ÿฌ์˜ ์›์ธ ๋ถ„์„์™€ ์ด์œ  * ๐Ÿšฉ: literal type์˜ ์˜ˆ์‹œ ์ฝ”๋“œ
1 parent 3579dbf commit f745d48

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

โ€Žsrc/app.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
function combine(n1: number | string, n2: number | string) {
1+
function combine(
2+
n1: number | string,
3+
n2: number | string,
4+
resultConversion: 'as-number' | 'as-text', // ์ด๋ ‡๊ฒŒ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
5+
) {
26
let result;
3-
if (typeof n1 === 'number' && typeof n2 === 'number') {
4-
result = n1 + n2;
7+
if (typeof n1 === 'number' && typeof n2 === 'number' || resultConversion === 'as-number') {
8+
result = +n1 + +n2; //error ts(2365) -> n1, n2์™ธ ๋‹ค๋ฅธ ์ธ์ž ์ถ”๊ฐ€๋˜๋ฉด์„œ ์กฐ๊ฑด๋ฌธ์—์„œ์˜ ๋…ผ๋ฆฌ์  ํ๋ฆ„์ด ์ถฉ๋ถ„ํžˆ ๋ช…ํ™•ํ•˜์ง€ ์•Š์•„ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ฝ”๋“œ์—์„œ TypeScript๋Š” n1๊ณผ n2๊ฐ€ string ํƒ€์ž…์ผ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค๊ณ  ํŒ๋‹จ, ์ธ์ž๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ ๋” ๋ช…ํ™•ํ•œ ํƒ€์ž…์„ ์š”๊ตฌ ๋”ฐ๋ผ '+'๋ฅผ ์ถ”๊ฐ€ํ•จ, ์ด๋Š” ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…๊ณผ๋Š” ๋ฌด๊ด€ํ•˜๋‹ค.
59
} else if (typeof n1 === 'string' && typeof n2 === 'string') {
610
result = n1 + n2;
711
} else {
812
result = n1.toString() + n2.toString();
9-
} // ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•: if๋ฌธ์œผ๋กœ typeof๋กœ ๊ฐ ํƒ€์ž…๋“ค์ด ๋ฌด์—‡์ธ์ง€ ์‹๋ณ„ํ•œ ํ›„ ์—ฐ์‚ฐ์„ ์ ์šฉํ•œ๋‹ค. ์—ฐ์‚ฐ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ํƒ€์ž…๋“ค์€ ๋ชจ๋‘ string์œผ๋กœ ํ˜•๋ณ€ํ™˜ ํ›„ ์—ฐ์‚ฐ์„ ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์œ ๋‹ˆ์˜จ ํƒ€์ž…์œผ๋กœ ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๊ฒƒ๋“ค์„ ํ™œ์šฉํ•  ๋•Œ๋Š” ์ •ํ™•ํžˆ ์–ด๋–ค ํƒ€์ž…์„ ๋ฐ›๋Š”์ง€์— ๊ด€ํ•œ ๋กœ์ง์ด ๋ณดํ†ต ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค ๊ตฌ์กฐ์— ๋งŽ์ด ํ•„์š”ํ•˜๋‹ค.
10-
return result;
13+
}
14+
// if (resultConversion === 'as-number') {
15+
// return +result;
16+
// } else {
17+
// return result.toString();
18+
// }
1119
}
12-
// Ts๊ฐ€ ์—ผ๋ คํ•˜๋Š” error์˜ ๊ทผ์›์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
13-
// const b1: boolean = true;
14-
// const b2: boolean = false;
15-
// console.log(b1 + b2);
20+
// ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์ด๋ž€? ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์€ ํŠน์ • ๋ณ€์ˆ˜๋‚˜ ์–ด๋–ค ํƒ€์ž…์ด์–ด์•ผ ํ•˜๋Š”์ง€ ์ •์˜ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค. ๋‹จ์ง„ ์ •ํ™•ํžˆ ์–ด๋–ค ๊ฐ’์ธ์ง€ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งˆ์šฐ์Šค๋ฅผ ์›ํ•˜๋Š” ๊ฐ’์— ํ˜ธ๋ฒ„ํ•˜๋ฉด ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€ ํ™•์ธ์ด ๋œ๋‹ค. Ts๋Š” ์•Œ์•„๊ฐˆ ์ˆ˜๋ก ์ ์  ํƒ€์ž…๋“ค์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์ขํ˜€๊ฐ€๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.
21+
const combineAges = combine(20, 30, 'as-number');
22+
console.log(combineAges);
1623

17-
const CombineAges = combine(20, 30);
18-
console.log(CombineAges);
24+
const combinedStringAges = combine('20', '30', 'as-number');
25+
console.log(combinedStringAges);
1926

20-
const CombineNames = combine('Max', 'Anna');
21-
console.log(CombineNames);
27+
const combineNames = combine('Max', 'Anna', 'as-text');
28+
console.log(combineNames);

0 commit comments

Comments
ย (0)