diff --git a/ts_playground/06-union-type.ts b/ts_playground/06-union-type.ts new file mode 100755 index 0000000..a857bf2 --- /dev/null +++ b/ts_playground/06-union-type.ts @@ -0,0 +1,11 @@ +type Input = string | number; + +function double(input: Input): number { + if (typeof input === "string") + return parseFloat(input) * 2; + + return input * 2; +} + +console.log(double(21)); +console.log(double("31")); \ No newline at end of file diff --git a/ts_playground/07-utility-types.ts b/ts_playground/07-utility-types.ts new file mode 100755 index 0000000..4d02100 --- /dev/null +++ b/ts_playground/07-utility-types.ts @@ -0,0 +1,25 @@ +type User2 = { + id: number; + name: string; + email: string; + password: string; +}; + +// 수정용 DTO에서 Partial로 일부만 허용 +type UpdateUserDto = Partial; + +// 공개용 DTO에서 비밀번호 제거 +type PublicUser = Omit; + +const updateUser: UpdateUserDto = { + name: "update user", +}; + +const publicUser: PublicUser = { + id: 135, + name: "public user", + email: "pu@dor.com" +}; + +console.log(updateUser); +console.log(publicUser); \ No newline at end of file diff --git a/ts_playground/08-record.ts b/ts_playground/08-record.ts new file mode 100755 index 0000000..ed4e306 --- /dev/null +++ b/ts_playground/08-record.ts @@ -0,0 +1,10 @@ +type statusCode = "OK" | "ERROR" | "NOT_FOUND"; + +// 매핑 +const statMessage: Record = { + OK: "정상 처리", + ERROR: "에러 발생", + NOT_FOUND: "찾을 수 없음" +}; + +console.log(statMessage.NOT_FOUND); \ No newline at end of file diff --git a/ts_playground/09-narrowing.ts b/ts_playground/09-narrowing.ts new file mode 100755 index 0000000..cd1d644 --- /dev/null +++ b/ts_playground/09-narrowing.ts @@ -0,0 +1,10 @@ +function print(value: string | string[]) { + if (Array.isArray(value)) { + console.log("Array: ", value.join(", ")); + } else { + console.log("Single value: ", value); + } +} + +print("hello"); +print(["hello", "world"]); \ No newline at end of file diff --git a/ts_playground/10-keyof-typeof.ts b/ts_playground/10-keyof-typeof.ts new file mode 100755 index 0000000..acbfedf --- /dev/null +++ b/ts_playground/10-keyof-typeof.ts @@ -0,0 +1,21 @@ +type User3 = { + id: number; + name: string; +}; + +type User3Keys = keyof User3; + +function getValue(user: User3, key: User3Keys) { + return user[key]; +} + +const user3 = { id: 10, name: "dor" }; +console.log(getValue(user3, "name")); + +const config = { + port: 3000, + debug: true +}; + +type Config = typeof config; +console.log(typeof config); \ No newline at end of file diff --git a/ts_playground/11-as-const.ts b/ts_playground/11-as-const.ts new file mode 100755 index 0000000..b2ffda1 --- /dev/null +++ b/ts_playground/11-as-const.ts @@ -0,0 +1,8 @@ +const ROLES = ["admin", "user", "guest"] as const; +type Role = typeof ROLES[number]; + +function checkRole(role: Role) { + console.log(`${role} 권한 확인`); +} + +checkRole("admin"); \ No newline at end of file diff --git a/ts_playground/12-unknown-never.ts b/ts_playground/12-unknown-never.ts new file mode 100755 index 0000000..82051b8 --- /dev/null +++ b/ts_playground/12-unknown-never.ts @@ -0,0 +1,18 @@ +function safeParse(json: string): unknown { + try { + return JSON.parse(json); + } catch { + return null; + } +} + +const result = safeParse('{"name":"Dorr"}'); +console.log(result); + +if (typeof result === "object" && result !== null && "name" in result) { + console.log((result as { name: string }).name); +} + +function fail(): never { + throw new Error("Failed!"); +} \ No newline at end of file diff --git a/ts_playground/tsconfig.json b/ts_playground/tsconfig.json index e8bec7a..374c29c 100755 --- a/ts_playground/tsconfig.json +++ b/ts_playground/tsconfig.json @@ -12,7 +12,7 @@ /* Language and Environment */ "target": "es2023", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "lib": ["ES2020"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "libReplacement": true, /* Enable lib replacement. */ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */