02-utility-types.ts
Typescript/src/02-intermediate/02-utility-types.ts
// ============================================
// 02. Utility Types
// ============================================
interface User {
id: number;
name: string;
email: string;
age: number;
}
// Partial<T> - Makes all properties optional
type PartialUser = Partial<User>;
// { id?: number; name?: string; email?: string; age?: number; }
function updateUser(user: User, updates: Partial<User>): User {
return { ...user, ...updates };
}
const user: User = { id: 1, name: "Alice", email: "alice@example.com", age: 25 };
const updated = updateUser(user, { age: 26 });
// Required<T> - Makes all properties required
interface Config {
apiUrl?: string;
timeout?: number;
}
type RequiredConfig = Required<Config>;
// { apiUrl: string; timeout: number; }
// Readonly<T> - Makes all properties readonly
type ReadonlyUser = Readonly<User>;
// { readonly id: number; readonly name: string; ... }
const readonlyUser: ReadonlyUser = { id: 1, name: "Alice", email: "alice@example.com", age: 25 };
// readonlyUser.name = "Bob"; // Error: Cannot assign to 'name' because it is a read-only property
// Pick<T, K> - Select specific properties
type UserPreview = Pick<User, "name" | "email">;
// { name: string; email: string; }
// Omit<T, K> - Remove specific properties
interface UserWithPassword extends User {
password: string;
}
type PublicUser = Omit<UserWithPassword, "password">;
// { id: number; name: string; email: string; age: number; }
// Record<K, T> - Create object type with specific keys and values
type UserRoles = Record<string, boolean>;
// { [key: string]: boolean; }
const roles: UserRoles = {
admin: true,
user: false,
guest: false
};
// With specific keys
type Status = "pending" | "approved" | "rejected";
type StatusMap = Record<Status, number>;
// { pending: number; approved: number; rejected: number; }
const statusCounts: StatusMap = {
pending: 5,
approved: 10,
rejected: 2
};
// Exclude<T, U> - Exclude types from a union
type T0 = Exclude<"a" | "b" | "c", "a">;
// "b" | "c"
type T1 = Exclude<string | number | (() => void), Function>;
// string | number
// Extract<T, U> - Extract types from a union
type T2 = Extract<"a" | "b" | "c", "a" | "f">;
// "a"
type T3 = Extract<string | number | (() => void), Function>;
// () => void
// NonNullable<T> - Remove null and undefined
type T4 = NonNullable<string | number | undefined>;
// string
type T5 = NonNullable<string[] | null | undefined>;
// string[]
// ReturnType<T> - Get return type of a function
function getUser() {
return { name: "Alice", age: 25 };
}
type UserReturn = ReturnType<typeof getUser>;
// { name: string; age: number; }
// Parameters<T> - Get parameters of a function as a tuple
function createUser(name: string, age: number, email: string) {
return { name, age, email };
}
type CreateUserParams = Parameters<typeof createUser>;
// [string, number, string]
// InstanceType<T> - Get instance type of a constructor
class UserClass {
constructor(public name: string) {}
}
type UserInstance = InstanceType<typeof UserClass>;
// UserClass
// Combining Utility Types
type UserUpdate = Partial<Omit<User, "id">>;
// { name?: string; email?: string; age?: number; }
export { updateUser, roles, statusCounts };
相關文章
01-introduction.ts
01-introduction.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/01-introduction.ts).
閱讀文章 →02-basic-types.ts
02-basic-types.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/02-basic-types.ts).
閱讀文章 →03-variables.ts
03-variables.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/03-variables.ts).
閱讀文章 →04-functions.ts
04-functions.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/04-functions.ts).
閱讀文章 →05-interfaces.ts
05-interfaces.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/05-interfaces.ts).
閱讀文章 →06-classes.ts
06-classes.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/06-classes.ts).
閱讀文章 →