- 제네릭 타입이라고도 불림
- 꼭 쓰지는 않아도 되지만, 짧게 사용할 수 있음, 정의한 인터페이스를 변환
Partial
- 특정 타입의 부분집합을 만족하는 타입을 정의할 수 있다
예시1.
interface Address {
email: string;
address: string;
}
type MyEmail = Partial<Address>;
const me: MyEmail = {};
const you: MyEmail = { email: "noh5524@gmail.com" };
const all: MyEmail = { email: "noh5524@gmail.com", address: "secho" };
interface UserProfile {
username: string;
email: string;
profilePhotoUrl: string;
}
type partials = Partial<UserProfile>
type UserProfileUpdate = {
username?: UserProfile["username"];
email: UserProfile["email"];
profilePhotoUrl?: UserProfile["profilePhotoUrl"];
};
type UserProfileUpdate = {
[p in 'username' | 'email' | 'profilePhotoUrl']? = UserProfile[p]
};
type UserProfileKeys = keyof UserProfile
type UserProfileUpdate = {
[p in key of UserProfile]? = UserProfile[p]
};
type RealPartial<T> = {
[p in key of T]? = T[p]
};
Omit
- 특정 속성만 제외한 타입 정의 (pick의 반대)
interface Product {
id: number;
name: string;
price: number;
brand: string;
stock: number;
}
type shoppingItem = Omit<Product, "stock">;
const apple: Omit<Product, "stock"> = {
id: 1,
name: "red apple",
price: 1000,
brand: "del"
};
const apple: Omit<Product, "stock" | "brand"> = {
id: 1,
name: "red apple",
price: 1000,
};
Pick
- 특정 타입에서 원하는 속성을 선택하여 타입 정의
interface Product {
id: number;
name: string;
price: number;
brand: string;
stock: number;
}
function fetchProduct(): Promise<Product[]> {
}
type shoppingItem = Pick<Product, "id" | "name" | "price">;
function displayProductDetail(shoppingItem: shoppingItem) {
}
interface UpdateProduct {
id?: number;
name?: string;
price?: number;
brand?: string;
stock?: number;
}
function updateProductItem(prodictItem: Partial<Product>) {
}