typescript 5.0์ด ๋์๋ค๊ณ ํ์ฌ ๊ฐ๋จํ ์ด๋ค ๊ธฐ๋ฅ๋ค์ด ์ถ๊ฐ๋์๋์ง ์ดํด๋ณด์๋ค. ์์ธํ ๋ด์ฉ์ Typescript 5.0 ๋ฒ์ญ์์ ํ์ธ ๊ฐ๋ฅํ๋ค.
๋ฐ์ฝ๋ ์ดํฐ
๋ฐ์ฝ๋ ์ดํฐ๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฐฉ์์ผ๋ก ํด๋์ค์ ๊ทธ ๋ฉค๋ฒ๋ฅผ ์ฌ์ฉ์ ์ ์ํ๋ ECMAScript์ ๊ธฐ๋ฅ์ด๋ค. ๋ฐ์ฝ๋ ์ดํฐ๋ ์ด์ ๋ถํฐ ์ง์๋์์ผ๋ Typescript 5.0๋ถํฐ๋ ๊ณต์ ๊ธฐ๋ฅ์ผ๋ก ์ง์ํ๋ค.
function loggedMethod(originalMethod: any, context: ClassMethodDecoratorContext) {
const methodName = String(context.name);
function replacementMethod(this: any, ...args: any[]) {
console.log(`LOG: Entering method '${methodName}'.`)
const result = originalMethod.call(this, ...args);
console.log(`LOG: Exiting method '${methodName}'.`)
return result;
}
return replacementMethod;
}
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
@loggedMethod
greet() {
console.log(`Hello, my name is ${this.name}.`);
}
}
const p = new Person("Ron");
p.greet();const ํ์ ํ๋ผ๋ฏธํฐ
ํน์ ๋ฌธ์์ด๋ก ํ์
์ ๊ตฌ์ฑํ๊ณ ์ถ์ ๋, ์ฃผ๋ก as const๋ฅผ ์ฌ์ฉํ์๋ค.
// The type we wanted:
// readonly ["Alice", "Bob", "Eve"]
// The type we got:
// string[]
const names1 = getNamesExactly({ names: ["Alice", "Bob", "Eve"]});
// Correctly gets what we wanted:
// readonly ["Alice", "Bob", "Eve"]
const names2 = getNamesExactly({ names: ["Alice", "Bob", "Eve"]} as const);ํ์ง๋ง ์ด์ ๋ ์ ๋ค๋ฆญ์ const๋ฅผ ์ถ๊ฐํ์ฌ ๋น์ทํ ํจ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
type HasNames = { names: readonly string[] };
function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
// ^^^^^
return arg.names;
}
// Inferred type: readonly ["Alice", "Bob", "Eve"]
// Note: Didn't need to write 'as const' here
const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"] });extends์์ ์ฌ๋ฌ ํ์ผ ๊ฐ์ ธ์ค๊ธฐ ๊ฐ๋ฅ
typescript 5.0๋ถํฐ๋ ๋ค์๊ณผ ๊ฐ์ด tsconfig.json์ extends์์ ์ฌ๋ฌ ํ์ผ์ ์์ฑํ ์ ์๋ค. ๋ฐ๋ผ์ ์ฌ๋ฌ ํ์ผ์ ํ์ฅํ ์ ์๊ฒ ๋๋ค.
{
"extends": ["a", "b", "c"],
"compilerOptions": {
// ...
}
}c๋ b๋ฅผ ํ์ฅํ๊ณ , b๋ a๋ฅผ ํ์ฅํ๋ ๊ฒ๊ณผ ๋น์ทํ ํจ๊ณผ๋ฅผ ๊ฐ์ง๋ค. ๋ฐ๋ผ์ ์ค์ ์ด ์ถฉ๋๋๋ ๊ฒฝ์ฐ ํ์์ ํญ๋ชฉ์ด ์ฐ์ ๋๋ค.
๋ชจ๋ enum์ ์ ๋์จ enum์ด๋ค.
typescript 2.0์์ enum literal type์ด ๋์ ๋๋ฉด์ enum์ ์ข ๋ ํน๋ณํด์ก๋ค. enum literal type์ ๊ฐ enum ๋ฉค๋ฒ์ ๊ณ ์ ํ ํ์ ์ ๋ถ์ฌํ๊ณ enum ์์ฒด๋ฅผ ๊ฐ ๋ฉค๋ฒํ์ ์ union์ผ๋ก ๋ง๋ค์๋ค.
// Color is like a union of Red | Orange | Yellow | Green | Blue | Violet
enum Color {
Red, Orange, Yellow, Green, Blue, /* Indigo */, Violet
}
// Each enum member has its own type that we can refer to!
type PrimaryColor = Color.Red | Color.Green | Color.Blue;
function isPrimaryColor(c: Color): c is PrimaryColor {
// Narrowing literal types can catch bugs.
// TypeScript will error here because
// we'll end up comparing 'Color.Red' to 'Color.Green'.
// We meant to use ||, but accidentally wrote &&.
return c === Color.Red && c === Color.Green && c === Color.Blue;
}์ด๋ ๋ฌธ์ ๋ ๋ฉค๋ฒ ํ์ ์ด ์ค์ ๊ฐ๊ณผ ์ผ๋ถ ์ฐ๊ด๋์ด์๋ค๋ ๊ฒ์ด๋ค. ๊ทธ๋์ ๋ฉค๋ฒ๊ฐ ํจ์ ํธ์ถ๋ก ์ด๊ธฐํ๋๋ฉด ๊ฐ์ ๊ณ์ฐํ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
enum E {
Blah = Math.random()
}Typescript 5.0 ์ ๊ณ์ฐ๋ ๊ฐ ๋ฉค๋ฒ์ ๋ํด ๊ณ ์ ํ ํ์ ์ ์์ฑํ์ฌ ๋ชจ๋ enum์ ๊ณต์ฉ์ฒด enum์ผ๋ก ๋ง๋ค ์ ์๋ค.
// typescript 5.0
const a : E.Blah = 10; // ๋ฌธ์ ์์
// typescript 5.0 ๋ฏธ๋ง
const a : E.Blah = 10; // Enum type 'E' has members with initializers that are not literals.โmoduleResolution bundler
typesciprt 4.7์๋ --module, --moduleResolution์ node16, nodenext ์ต์
์ ๋์
ํ์ฌ ECMAScript ๋ชจ๋์ ๋ํ ์ ํํ ์กฐํ ๊ท์น์ ๋ชจ๋ธ๋งํ ์ ์๊ฒ ๋์๋ค. ํ์ง๋ง ์ด๋ ๋ช๋ช ๊ฐ๋ฐ์์๊ฒ ๋ฒ๊ฑฐ๋ก์ด ์ค์ ์ด์๋ค.
5.0์์๋ compilerOptionsdp moduleResolution์ bundler ์ต์
์ ์ถ๊ฐํ์ฌ ๋ฒ๋ค๋ฌ์ ๋์์ ๋ชจ๋ธ๋งํ ์ ์๋ค.
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "bundler"
}
}ํ์ด๋ธ๋ฆฌ๋ ์กฐํ ์ ๋ต์ ๊ตฌํํ๋ Vite, esbuild, swc, Webpack, Parcel ๋ฑ์ ์ต์ ๋ฒ๋ค๋ฌ๋ฅผ ์ฌ์ฉ ์ค์ด๋ผ๋ฉด ์๋ก์ด bundler์ต์
์ด ์ ํฉํ ๊ฒ์ด๋ค. ํ์ง๋ง, ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ์์์ผ ๊ฒฝ์ฐ bundler ์ต์
์ ์ฌ์ฉํ๋ฉด, ๋ฒ๋ค๋ฌ๋ฅผ ์ฌ์ฉํ์ง ์๋ ์ฌ์ฉ์์๊ฒ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์์ผ๋ node16, nodenext ์ต์
์ ์ถ์ฒํ๋ค.
Resolution ์ปค์คํฐ๋ง์ด์ง ํ๋๊ทธ
- allowImportingTsExtensions :
.ts,.mts,.tsx์ ๊ฐ์ด Typescript ์ ์ฉ ํ์ฅ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก๋ฅผ import ํ ์ ์๋ค. - resolvePackageJsonExports : typescript๊ฐ
node_modules์ ํจํค์ง๋ฅผ ์ฝ์ ๊ฒฝ์ฐ, package.json์ exports ํ๋๋ฅผ ์ฐธ์กฐํ๋๋ก ํ๋ค. - resolvePackageJsonImports : package.json์ ํฌํจํ๋ ์กฐ์ ๋๋ ํฐ๋ฆฌ์ ํ์ผ๋ก๋ถํฐ
#์ผ๋ก ์์ํ๋ ์กฐํ๋ฅผ ์ํํ ๋, typescript๊ฐ package.json์ import ํ๋๋ฅผ ์ฐธ์กฐํ๋๋ก ๊ฐ์ ํจ - allowArbitraryExtensions
- typescript 5.0์์ import ๊ฒฝ๋ก๊ฐ js๋๋ ts ํ์ผ ํ์ฅ์๊ฐ ์๋ ๊ฒฝ์ฐ
{ํ์ผ ๊ธฐ๋ณธ ์ด๋ฆ}.d.{ํ์ฅ์}.tsํ์์ ํด๋น ๊ฒฝ๋ก์์ ํ์ ์ ์ฐพ๋๋ค. - Typescript์์๋ ์ด ํ์ผ ํ์์ ์ดํดํ์ง ๋ชปํ์ฌ ๋ฐํ์์์ import๋ฅผ ์ง์ํ์ง ์์ ์ ์์์ ์๋ฆฌ๋ ์ค๋ฅ๋ฅผ ๋ด์ง๋ง, ์ด ํ๋๊ทธ๋ฅผ ํตํด ์ต์ ํ ์ ์๋ค.
- typescript 5.0์์ import ๊ฒฝ๋ก๊ฐ js๋๋ ts ํ์ผ ํ์ฅ์๊ฐ ์๋ ๊ฒฝ์ฐ
customConditions
package.json์ exports, imports ํ๋๋ฅผ Typescript๊ฐ ๋ฆฌ์กธ๋ธํ ๋ ์ฑ๊ณตํด์ผํ๋ ์ถ๊ฐ ์กฐ๊ฑด์ ๋ชฉ๋ก์ ๋ฐ๋๋ค. ํ์กดํ๋ ์กฐ๊ฑด์ ์ถ๊ฐ๋๋ค.
{
"compilerOptions": {
"target": "es2022",
"moduleResolution": "bundler",
"customConditions": ["my-condition"]
}
}{
// ...
"exports": {
".": {
"my-condition": "./foo.mjs",
"node": "./bar.mjs",
"import": "./baz.mjs",
"require": "./biz.mjs"
}
}
}์์ ๊ฐ์ ๊ฒฝ์ฐ exports, imports ํ๋๋ฅผ ๋ฆฌ์กธ๋ธํ ๋ ํญ์ my-condition์ ๊ณ ๋ คํ๋ค.
โverbatimModuleSyntax
๊ธฐ๋ณธ์ ์ผ๋ก Typescript๋ import elision์ ์ํํ๋ค. ์๋ฅผ๋ค์ด ๋ค์๊ณผ ๊ฐ์ด import๋ฅผ type์ ๊ฐ์ ธ์ค๊ธฐ๋ง์ ์ํด ์ฌ์ฉํ๋ค๋ฉด, ์ด๋ฅผ ์ง์ด๋ค.
import { Car } from "./car";
export function drive(car: Car) {
// ...
}// import ๋ฌธ์ ์ง์
export function drive(car) {
// ...
}type์ ๊ฐ์ด ์๋๊ธฐ์ ์ง์ฐ์ง ์์ผ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ํ์ง๋ง ๋ช๊ฐ์ง ์ฃ์ง ์ผ์ด์ค๊ฐ ์์ ์ ์๋ค. ์๋ฅผ๋ค์ด ๋ณํ๋ ์ฝ๋๋ import ".car"์กฐ์ฐจ ๋จ๊ธฐ์ง ์๋๋ฐ, ์ด๋ ์ฌ์ด๋ ์ดํํธ๋ฅผ ์ผ์ผํฌ ์๋ ์๋ค.
๋ฐ๋ผ์ Typescript์์๋ type ์์ ์๋ฅผ ์ฌ์ฉํ๋๋ก ํ๋ --importsNotUsedAsValues, ์ผ๋ถ ๋ชจ๋ ์๋ฆฌ์
๋์์ ๋ฐฉ์งํ๋ --perserveValueImports, Typescript๊ฐ ์ฌ๋ฌ ์ปดํ์ผ๋ฌ์์ ์๋ํ๋์ง ํ์ธํ๋ --isolatedModules ํ๋๊ทธ๊ฐ ์๋ค. ํ์ง๋ง ์ด ํ๋๊ทธ๋ค์ ์ดํดํ๊ธฐ ์ด๋ ต๊ณ , ์์ง ์ผ์ด์ค ์ญ์ ์กด์ฌํ๋ค.
Typescript 5.0์์๋ --verbatimModuleSyntax ๋ฅผ ์ ์ฉํ์ฌ type ์์ ์๊ฐ ์๋ ๋ชจ๋ import, export๋ ๊ทธ๋๋ก ์ ์งํ๋๋ก ํ๋ค. type ์์ ์๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ํญ๋ชฉ์ ์์ ํ ์ญ์ ๋๋ค.
// Erased away entirely.
import type { A } from "a";
// Rewritten to 'import { b } from "bcd";'
import { b, type c, type d } from "bcd";
// Rewritten to 'import {} from "xyz";'
import { type xyz } from "xyz";ํ์ง๋ง ์ด ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ๋ฉด ECMAScript import์ export๋, ๋ค๋ฅธ ๋ชจ๋ ์์คํ
์ ์์ํ๋ ํ์ผ ํ์ฅ์ ๋๋ ์ธํ
์ด ์์๋ require ๋ก ์ฌ์์ฑ๋์ง ์๋๋ค. ๋์ ์ค๋ฅ๋ฅผ ๋ธ๋ค. ๋ง์ฝ require๋ module.exports๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ฅผ emit ํ๊ณ ์ถ๋ค๋ฉด ES2015 ์ด์ ์ Typescript ๋ชจ๋ ๊ตฌ๋ฌธ์ ์ฌ์ฉํด์ผํ๋ค.
| Input Typescript | Output Javascript |
|---|---|
import foo = require("foo"); | const foo = require("foo"); |
| function foo() {} function bar() {} function baz() {} export = { foo, bar, baz }; | function foo() {} function bar() {} function baz() {} module.exports = { foo, bar, baz }; |
์ด๋ ๋ช๋ช ์ํฉ์์ ๋์์ด ๋๋๋ฐ, ์๋ฅผ๋ค์ด ๊ฐ๋ฐ์๊ฐ package.json์ --module node16์ ์ค์ ํ์ง ์์ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๊ฐ๋ฐ์๋ ์ธ์งํ์ง ๋ชปํ์ฑ ES ๋ชจ๋ ๋์ CommonJS ๋ชจ๋์ ์์ฑํ๊ธฐ ์์ํ์ฌ ์์์น ๋ชปํ ์กฐํ ๊ท์น๊ณผ JavaScript ์ถ๋ ฅ์ ์ ๊ณตํ๊ฒ ๋๋ค. ์ด ํ๋๊ทธ๋ ๊ตฌ๋ฌธ์ด ์๋์ ์ผ๋ก ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์ค์ธ ํ์ผ ํ์์ ๋ํด ์๋์ ์ผ๋ก ํ์ธํ ์ ์๋ค.
-verbatimModuleSyntax์ --importsNotUsedAsValues ๋ฐ --preserveValueImports๋ณด๋ค ๋ ์ผ๊ด๋ ์คํ ๋ฆฌ๋ฅผ ์ ๊ณตํ๋ฏ๋ก, ๊ธฐ์กด์ ๋ ํ๋๊ทธ๋ ์ด ๊ตฌ๋ฌธ์ผ๋ก ๋์ฒด๋๋ค.
Support for export type *
๊ธฐ์กด์ export * from "module"์ด๋ export * as ns from "module" ์ฒ๋ผ re-export๊ฐ ๋ถ๊ฐ๋ฅํ๋ค. Typescript 5.0 ๋ถํฐ๋ ๊ฐ๋ฅํ๋ค.
// models/vehicles.ts
export class Spaceship {
// ...
}
// models/index.ts
export type * as vehicles from "./vehicles";
// main.ts
import { vehicles } from "./models";
function takeASpaceship(s: vehicles.Spaceship) {
// โ
ok - `vehicles` only used in a type position
}
function makeASpaceship() {
return new vehicles.Spaceship();
// ^^^^^^^^
// 'vehicles' cannot be used as a value because it was exported using 'export type'.
}@satisfies Support in JSDoc
Typescript 4.9์์ satisfies ์ฐ์ฐ์๊ฐ ์ถ๊ฐ๋๋ค. ์ด ์ฐ์ฐ์๋ ํ์
์ ์ํฅ์ ์ฃผ์ง ์๊ณ , ํํ์์ด ํ์
์ ํธํ๋๋์ง๋ง ํ์ธํ๋ค.
interface CompilerOptions {
strict?: boolean;
outDir?: string;
// ...
}
interface ConfigSettings {
compilerOptions?: CompilerOptions;
extends?: string | string[];
// ...
}
let myConfigSettings = {
compilerOptions: {
strict: true,
outDir: "../lib",
// ...
},
extends: [
"@tsconfig/strictest/tsconfig.json",
"../../../tsconfig.base.json"
],
} satisfies ConfigSettings;์ ์ฝ๋์์ Typescript๋ myConfigSettings.extends๊ฐ ๋ฐฐ์ด์์ ์๋๋ฐ, ์ด๋ ConfigSettings์ ํธํ ๊ฐ๋ฅํ์ง๋ง ํ์ธํ๊ณ , myConfigSettings์ ํ์
์ ๋ณ๊ฒฝํ์ง๋ ์์๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ extends์ map ํจ์๋ฅผ ์ฌ์ฉํด๋ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค.
์ด satisfies๊ฐ JSDoc ํ๊ทธ์ ์ถ๊ฐ๋์๋ค.
// @ts-check
/**
* @typedef CompilerOptions
* @prop {boolean} [strict]
* @prop {string} [outDir]
*/
/**
* @satisfies {CompilerOptions}
*/
let myCompilerOptions = {
outdir: "../lib",
// ~~~~~~ oops! we meant outDir
};๋ค์๊ณผ ๊ฐ์ด /** @satisfies */ ์ธ๋ผ์ธ์ผ๋ก ์ฌ์ฉํ๊ฑฐ๋, ํจ์ ํธ์ถ๊ณผ ๊ฐ์ ๊ณณ์์๋ ์ฌ์ฉํ ์ ์๋ค.
let myConfigSettings = /** @satisfies {ConfigSettings} */ ({
compilerOptions: {
strict: true,
outDir: "../lib",
},
extends: [
"@tsconfig/strictest/tsconfig.json",
"../../../tsconfig.base.json"
],
});
// ํจ์ ํธ์ถ์์ ์ฌ์ฉ
compileCode(/** @satisfies {ConfigSettings} */ ({
// ...
}));@overload Support in JSDoc
Typescript์์๋ ํจ์ ์ค๋ฒ๋ก๋๋ฅผ ๋ง๋ค ์ ์๋ค. ์ด๋ก์จ ํจ์๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ ํํ ์ ์๋ค.
// Our overloads:
function printValue(str: string): void;
function printValue(num: number, maxFractionDigits?: number): void;
// Our implementation:
function printValue(value: string | number, maximumFractionDigits?: number) {
if (typeof value === "number") {
const formatter = Intl.NumberFormat("en-US", {
maximumFractionDigits,
});
value = formatter.format(value);
}
console.log(value);
}์ด๋ฌํ ์ค๋ฒ๋ก๋ ๊ธฐ๋ฅ์ @overload ํ๊ทธ๋ฅผ ์ฌ์ฉํด์ JSDoc ์์์ ์ฌ์ฉํ ์ ์๋ค.
// @ts-check
/**
* @overload
* @param {string} value
* @return {void}
*/
/**
* @overload
* @param {number} value
* @param {number} [maximumFractionDigits]
* @return {void}
*/
/**
* @param {string | number} value
* @param {number} [maximumFractionDigits]
*/
function printValue(value, maximumFractionDigits) {
if (typeof value === "number") {
const formatter = Intl.NumberFormat("en-US", {
maximumFractionDigits,
});
value = formatter.format(value);
}
console.log(value);
}Passing Emit-Specific Flags Under --build
--build ๋ชจ๋์์ ๋ค์ ํ๋๊ทธ๊ฐ ์ถ๊ฐ๋์๋ค. ๋น๋ ์ ์ปค์คํฐ๋ง์ด์ง์ ์ํด ์ฌ์ฉํ๋ค.
--declarationโ-emitDeclarationOnlyโ-declarationMapโ-sourceMapโ-inlineSourceMap
Case-Insensitive Import Sorting in Editors
VSCode ์ ๊ฐ์ ์ฝ๋์๋ํฐ์์ TypeScript๋ import, export ๋ชฉ๋ก ์ ๋ ฌ์ ์ง์ํ๋ค. ํ์ง๋ง Typescript๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ฌ ์ ๋ ฌํ๋ค. ์์คํค ๋ฌธ์ ์ธ์ฝ๋ฉ ์ ๋๋ฌธ์๋ ์๋ฌธ์ ์์ด๊ธฐ์ ๋ค์ ์ ๋ ฌ์ Typescript์์ ์ ์ ํ๋ค.
import {
Toggle,
freeze,
toBoolean,
} from "./utils";ํ์ง๋ง ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ง ์๊ณ ์ ๋ ฌํ๊ณ ์ถ์ ์๋ ์๋ค. ์ด์ ๋ organizeImports ์ต์
์ด ์ถ๊ฐ๋์ด ํด๋น ์ค์ ์ ํ ์ ์๋ค.
{
"typescript.unstable": {
// Should sorting be case-sensitive? Can be:
// - true
// - false
// - "auto" (auto-detect)
"organizeImportsIgnoreCase": "auto",
// Should sorting be "ordinal" and use code points or consider Unicode rules? Can be:
// - "ordinal"
// - "unicode"
"organizeImportsCollation": "ordinal",
// Under `"organizeImportsCollation": "unicode"`,
// what is the current locale? Can be:
// - [any other locale code]
// - "auto" (use the editor's locale)
"organizeImportsLocale": "en",
// Under `"organizeImportsCollation": "unicode"`,
// should upper-case letters or lower-case letters come first? Can be:
// - false (locale-specific)
// - "upper"
// - "lower"
"organizeImportsCaseFirst": false,
// Under `"organizeImportsCollation": "unicode"`,
// do runs of numbers get compared numerically (i.e. "a1" < "a2" < "a100")? Can be:
// - true
// - false
"organizeImportsNumericCollation": true,
// Under `"organizeImportsCollation": "unicode"`,
// do letters with accent marks/diacritics get sorted distinctly
// from their "base" letter (i.e. is รฉ different from e)? Can be
// - true
// - false
"organizeImportsAccentCollation": true
},
"javascript.unstable": {
// same options valid here...
},
}Exhaustive switch/case Completions
switch ๋ฌธ์ ์์ฑํ ๋ ๊ฒ์ฌ ๋์์ ๋ฆฌํฐ๋ด ํ์ ์ด ์๋์ง ๊ฐ์งํ๋ค. ๊ฐ์ง๋๋ฉด ๋ฐ๊ฒฌ๋์ง ์์ ๊ฐ ๋์๋ฌธ์๋ฅผ ์ค์นดํด๋ฉํ๋ ์๊ฒฐ์ฑ์ ์ ๊ณตํ๋ค.
type Fruit =
| { kind : "apple" }
| { kind : "orange" }
| { kind : "banana" }
function nom(fruit: Fruit) {
switch(fruit.kind) {
case // case ๋ฌธ์ ์์ฑํ๋ฉด ์๋ ์์ฑ์ผ๋ก case "apple": case "orange": case "banana"๊ฐ ์๊น
}
}Speed, Memory, and Package Size Optimizations
Typescript 5.0์์๋ ์ฝ๋ ๊ตฌ์กฐ, ๋ฐ์ดํฐ ๊ตฌ์กฐ, ์๊ณ ๋ฆฌ์ฆ ๊ตฌํ์ ๋ณ๊ฒฝํ์ฌ ์ ์ฒด ๊ฒฝํ์ ์ข ๋ ๋น ๋ฅด๊ฒ ๋ง๋ค์๋ค.


Breaking Changes and Deprecations
Runtime Requirements
Typescript 5.0๋ถํฐ๋ ECMAScript 2018์ ๋์์ผ๋ก ํ๋ค. ๋ํ, Typescipt ํจํค์ง๋ ์ต์ ์๊ตฌ ์์ง์ 12.20์ผ๋ก ์ค์ ํ๋ค.
lib.d.ts Changes
DOM ์ ํ์ด ์์ฑ๋๋ ๋ฐฉ์์ด ๋ณ๊ฒฝ๋์ด ๊ธฐ์กด ์ฝ๋์ ์ํฅ์ ๋ฏธ์น ์ ์๋ค. ํนํ ํน์ property๊ฐ ์ซ์์์ ์ซ์ ๋ฆฌํฐ๋ด ํ์ ์ผ๋ก ๋ณํ๋์๋ค. ์๋ผ๋ด๊ธฐ, ๋ณต์ฌ, ๋ถ์ฌ๋ฃ๊ธฐ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ์ํ ํ๋กํผํฐ์ ๋ฉ์๋๊ฐ ์ธํฐํ์ด์ค ์ ๋ฐ์ผ๋ก ์ด๋๋์๋ค.
API Breaking Changes
๋ชจ๋๋ก ์ ํํ๊ณ , ๋ถํ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ฑฐํ์ผ๋ฉฐ ์ผ๋ถ ์ ํ์ฑ์ ๊ฐ์ ํ๋ค.
Forbidden Implicit Coercions in Relational Operators
Typescript์ ํน์ ์ฐ์ฐ์ ์์์ ์ผ๋ก ๋ฌธ์์ด์ ์ซ์๋ก ๊ฐ์ ๋ณํํ ์ ์๋ ์ฝ๋๋ฅผ ์์ฑํ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ํ๊ณ ์๋ค.
function func(ns: number | string) {
return ns * 4; // Error, possible implicit coercion
}5.0 ๋ถํฐ๋ ๊ด๊ณ ์ฐ์ฐ์ >, <, โ, >= ์๋ ์ด ๊ธฐ๋ฅ์ด ์ ์ฉ๋๋ค. ์ํ๋ ๊ฒฝ์ฐ +๋ฅผ ์ฌ์ฉํ์ฌ ํผ์ฐ์ฐ์๋ฅผ ์ซ์๋ก ๋ช
์์ ์ผ๋ก ๊ฐ์ ํ ์ ์๋ค.
function func(ns: number | string) {
return ns > 4; // Now also an error
}
function func(ns: number | string) {
return +ns > 4; // OK
}Enum Overhaul
Enum์ ์ฌ์ฉํ ๋ Enum ์ ํ์ ๋๋ฉ์ธ ์ธ๋ถ ๋ฆฌํฐ๋ด์ ํ ๋นํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋๋ก ์ถ๊ฐ๋๋ค.
enum SomeEvenDigit {
Zero = 0,
Two = 2,
Four = 4
}
// Now correctly an error
let m: SomeEvenDigit = 1;๋ ์ซ์์ ๊ฐ์ string enum ์ฐธ์กฐ๊ฐ ํผํฉ๋์ด ์ ์ธ๋ ๊ฐ์ด ์๋ enum์์ ์ค๋ฅ๋ฅผ ์ ๋๋ก ์์ฑํ๋๋ก ๋ณ๊ฒฝ๋๋ค.
enum Letters {
A = "a"
}
enum Numbers {
one = 1,
two = Letters.A
}
// Now correctly an error
const t: number = Numbers.two;More Accurate Type-Checking for Parameter Decorators in Constructors Under --experimentalDecorators
decorator์ ๋ํ ์ข ๋ ์ ํํ type-checking์ด ๋๋๋ก ํ๋ ์ต์ ์ด ์ถ๊ฐ๋์๋ค. ํนํ ์์ฑ์ ํ๋ผ๋ฏธํฐ์์ ์ฌ์ฉํ ๋ ํจ๊ณผ์ ์ด๋ค.
export declare const inject:
(entity: any) =>
(target: object, key: string | symbol, index?: number) => void;
export class Foo {}
export class C {
constructor(@inject(Foo) private x: any) {
}
}key๊ฐ string | symbol์ ์์ํ๊ธฐ์ ์ด ํธ์ถ์ ์คํจํ์ง๋ง ์์ฑ์ ํ๋ผ๋ฏธํฐ๋ undefinedํค๋ฅผ ๋ฐ๋๋ค. ์ฌ๋ฐ๋ฅธ ์์ ์ inject์ key์ ํ์
์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด๋ค. ๋ง์ฝ ๋ณ๊ฒฝํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉ์ค์ธ ๊ฒฝ์ฐ์๋ inject๋ฅผ ์ข ๋ type-safeํ decorator๋ก ๊ฐ์ธ๊ณ key์ type assertion์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
Deprecations and Default Changes
์ด์ ๋ค์ ์ค์ ๊ฐ๋ค์ ์ฌ์ฉ๋์ง ์๋๋ค.
--target: ES3--out--noImplicitUseStrict--keyofStringsOnly--suppressExcessPropertyErrors--suppressImplicitAnyIndexErrors--noStrictGenericChecks--charset--importsNotUsedAsValues--preserveValueImportsprependin project references
Typescript 5.5๊น์ง๋ ํ์ฉํ์ง๋ง ์ดํ๋ก๋ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ๋ค. ๋จ, "ignoreDeperecations": "5.0"์ ์ฌ์ฉํ๋ฉด ๊ฒฝ๊ณ ๋ฅผ ์์จ ์ ์๋ค.
ํ๋ก์ ํธ ๋ด์์ ๋์ผํ ํ์ผ ์ด๋ฆ์ ์ฐธ์กฐํ๋ ๋ชจ๋ ์ฐธ์กฐ๊ฐ ์ผ์ด์ค์ ๋ํด ๋์ํ๋๋ก ๋ณด์ฅํ๋ --forceConsistentCasingInFileNames๋ ์ด์ true๋ก ๊ธฐ๋ณธ ์ค์ ๋๋ค.
์ถ์ฒ
https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#moduleresolution-bundler