๐Ÿ”ฅ๐Ÿ”ฅ Typescript ์ƒˆ ๋ฒ„์ „ ๊ธฐ๋Šฅ

TypeScript๋Š” Microsoft์—์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ์œ ์ง€ ๊ด€๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. JavaScript์˜ ์—„๊ฒฉํ•œ ๊ตฌ๋ฌธ ์ƒ์œ„ ์ง‘ํ•ฉ์ด๋ฉฐ ์„ ํƒ์ ์ธ ์ •์  ์œ ํ˜• ์ง€์ •์„ ์–ธ์–ด์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

Typescript๋Š” 2021๋…„ 4์›” 1์ผ์— ๋ฒ ํƒ€ ๋ฒ„์ „ 4.3์„ ๋ฐœํ‘œํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฒ„์ „์„ ์„ค์น˜ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
npm install typescript@beta
ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด ์œ ํ˜• ๊ฐœ์„  ์‚ฌํ•ญ
์ƒˆ ๋ฒ„์ „์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์œ ํ˜•์ด ์ง€์ •๋œ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ํ‘œํ˜„์‹์— ๋Œ€ํ•œ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์œ ํ˜•์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋Œ€์ƒ ์œ ํ˜•์„ ์œ ์ถ”ํ•  ๋•Œ TypeScript๋Š” ์ด์ œ ์†Œ์Šค ์œ ํ˜•์ด ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์œ ํ˜•์ด ๋˜๋„๋ก ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๊ฐœ์„ ๋œ ํ• ๋‹น ๊ด€๊ณ„์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ž…๋‹ˆ๋‹ค.

type Color = "red" | "blue";
type Quantity = "one" | "two";

type SeussFish = `${Quantity | Color} fish`;
// same as
//  type SeussFish = "one fish" | "two fish"
//         | "red fish" | "blue fish";
โ€ฆor match patterns of other string-like types.

declare let s1: `${number}-${number}-${number}`;
declare let s2: `1-2-3`;

// Works!
s1 = s2;


ํ•ญ์ƒ ์ง„์‹คํ•œ ์•ฝ์† ํ™•์ธ
์กฐ๊ฑด๋ฌธ์—์„œ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋ˆ„๋ฝ์— ๋Œ€ํ•œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ์— ๋Œ€ํ•œ ์ง€์›์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.



์ •์  ์ธ๋ฑ์Šค ์„œ๋ช…
์ธ๋ฑ์Šค ์„œ๋ช…์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ ํ˜•์ด ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์€ ์†์„ฑ์„ ๊ฐ’์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์‹  ๋ฒ„์ „์˜ ์ƒ‰์ธ ์„œ๋ช…์€ ์ด์ œ ์ •์ ์œผ๋กœ ์„ ์–ธ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆ˜์ž… ๋ช…์„ธ์„œ ์™„์„ฑ
๊ฐœ์„ ๋œ ์ž๋™ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฌธ, ๋” ์‰ฝ๊ณ  ์•ˆ์ •์ ์ž„, ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฌธ ์ž‘์„ฑ์„ ์‹œ์ž‘ํ•˜๋ฉด TypeScript๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ชฉ๋ก์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์™„๋ฃŒ๋ฅผ ์ปค๋ฐ‹ํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•˜๋ ค๋Š” ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ „์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฌธ์„ ์™„๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ตœ์‹  ๋ฒ„์ „์˜ TypeScript๋ฅผ ์‚ฌ์šฉํ•œ ์ž๋™ ๊ฐ€์ ธ์˜ค๊ธฐ์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค.

ํƒœ๊ทธ์— ๋Œ€ํ•œ ํŽธ์ง‘๊ธฐ ์ง€์›
TypeScript๋Š” ์ด์ œ ํƒœ๊ทธ๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์—ฐ๊ฒฐ๋œ ์„ ์–ธ์„ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ํƒœ๊ทธ ๋‚ด์˜ ์ด๋ฆ„ ์œ„๋กœ ๋งˆ์šฐ์Šค๋ฅผ ๊ฐ€์ ธ๊ฐ€ ๋น ๋ฅธ ์ •๋ณด๋ฅผ ์–ป๊ฑฐ๋‚˜ go-to-definition ๋˜๋Š” find-all-references์™€ ๊ฐ™์€ ๋ช…๋ น์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ์˜ˆ์—์„œ bar in bar ์ •์˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ TypeScript ์ง€์› ํŽธ์ง‘๊ธฐ๋Š” bar์˜ ํ•จ์ˆ˜ ์„ ์–ธ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

/**
 * This function depends on {@link bar}
 */
function foo() {

}

function bar() {

}



๊ณต์šฉ์ฒด ์—ด๊ฑฐํ˜•์€ ์ž„์˜์˜ ์ˆซ์ž์™€ ๋น„๊ตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
ํŠน์ • ์—ด๊ฑฐํ˜•์— ๋Œ€ํ•œ ํ•ด๊ฒฐ๋œ ๋ฌธ์ œ๋Š” ๊ตฌ์„ฑ์›์ด ์ž๋™์œผ๋กœ ์ฑ„์›Œ์ง€๊ฑฐ๋‚˜ ์‚ฌ์†Œํ•˜๊ฒŒ ์ž‘์„ฑ๋œ ๊ฒฝ์šฐ ํ†ตํ•ฉ ์—ด๊ฑฐํ˜•์œผ๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ์—ด๊ฑฐํ˜•์€ ์ž ์žฌ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ ๊ฐ’์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒˆ ๋ฒ„์ „์—์„œ๋Š” ์œ ๋‹ˆ์˜จ ์—ด๊ฑฐํ˜•์ด ์žˆ๋Š” ๋ณ€์ˆ˜๊ฐ€ ์ ˆ๋Œ€ ๊ฐ™์„ ์ˆ˜ ์—†๋Š” ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด๊ณผ ๋น„๊ต๋˜๋ฉด ์œ ํ˜• ๊ฒ€์‚ฌ๊ธฐ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

enum E {
 A = 0,
 B = 1,
}

function doSomething(x: E) {
 // Error! This condition will always return 'false' since the types 'E' and '-1' have no overlap.
 if (x === -1) {
  // ...
 }
}

//As a workaround, you can re-write an annotation to include the appropriate literal type.

// Include -1 in the type, if we're really certain that -1 can come through.
function doSomething(x: E | -1) {
 if (x === -1) {
  // ...
 }
}


//You can also use a type-assertion on the value.
function doSomething(x: E) {
 // Use a type asertion on 'x' because we know we're not actually just dealing with values from 'E'.
 if ((x as number) === -1) {
  // ...
 }
}


๋˜๋Š” ์—ด๊ฑฐํ˜•์„ ๋‹ค์‹œ ์„ ์–ธํ•˜์—ฌ ์ค‘์š”ํ•˜์ง€ ์•Š์€ ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ํ•ด๋‹น ์—ด๊ฑฐํ˜•์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๊ณ  ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. enum์ด ๋ช‡ ๊ฐ€์ง€ ์ž˜ ์•Œ๋ ค์ง„ ๊ฐ’์„ ์ง€์ •ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

enum E {
 // the leading + on 0 opts TypeScript out of inferring a union enum.
 A = +0,
 B = 1,
}


์ „์ฒด ๊ธฐ์‚ฌ๋ฅผ ์ฝ๊ณ  ์‹ถ๋‹ค๋ฉด ์—ฌ๊ธฐ์—์„œ ์ฒดํฌ์•„์›ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ–‰๋ณตํ•œ ์ฝ”๋”ฉ!

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ์—์„œ ๊ธฐ์‚ฌ๋ฅผ ํ™•์ธํ•˜์„ธ์š”:- Typescript New Version Features

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ