parent
7759fd246e
commit
40cec0dae1
@ -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!"); |
||||
} |
Loading…
Reference in new issue