main
syneff 7 days ago
parent 7759fd246e
commit 40cec0dae1
  1. 11
      ts_playground/06-union-type.ts
  2. 25
      ts_playground/07-utility-types.ts
  3. 10
      ts_playground/08-record.ts
  4. 10
      ts_playground/09-narrowing.ts
  5. 21
      ts_playground/10-keyof-typeof.ts
  6. 8
      ts_playground/11-as-const.ts
  7. 18
      ts_playground/12-unknown-never.ts
  8. 2
      ts_playground/tsconfig.json

@ -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"));

@ -0,0 +1,25 @@
type User2 = {
id: number;
name: string;
email: string;
password: string;
};
// 수정용 DTO에서 Partial로 일부만 허용
type UpdateUserDto = Partial<User2>;
// 공개용 DTO에서 비밀번호 제거
type PublicUser = Omit<User2, 'password'>;
const updateUser: UpdateUserDto = {
name: "update user",
};
const publicUser: PublicUser = {
id: 135,
name: "public user",
email: "pu@dor.com"
};
console.log(updateUser);
console.log(publicUser);

@ -0,0 +1,10 @@
type statusCode = "OK" | "ERROR" | "NOT_FOUND";
// 매핑
const statMessage: Record<statusCode, string> = {
OK: "정상 처리",
ERROR: "에러 발생",
NOT_FOUND: "찾을 수 없음"
};
console.log(statMessage.NOT_FOUND);

@ -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"]);

@ -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);

@ -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");

@ -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!");
}

@ -12,7 +12,7 @@
/* Language and Environment */ /* Language and Environment */
"target": "es2023", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "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. */ // "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */ // "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */

Loading…
Cancel
Save