interface Profile {
name: string;
age: number;
}
type P<T> = {
[key in keyof T]?: string | number;
}
const user:P<Profile> = {
name: "jiwoo"
}
interface Profile {
name: string;
age: number;
}
type P<T> = {
[key in keyof T]: string | number;
}
const user:Partial<Profile> = {
name: "jiwoo"
}
interface Profile {
name: string;
age: number;
married: boolean;
}
type P<T> = {
[key in keyof T]: string | number;
}
const user:Pick<Profile, 'name'|'age'> = {
name: "jiwoo",
age: 24
}
/*
interface Profile {
name: string;
age: number;
}
*/
type P<T,S extends keyof T> = {
[key in S ]: T[key];
}
const user:P<Profile,'name'|'age'> = {
name: "jiwoo",
age: 24
}
interface Profile {
name: string;
age: number;
married: boolean;
}
const user: Omit<Profile, "married"> = {
name: "jiwoo",
age: 24,
};
/*
interface Profile {
name: string;
age: number;
}
*/
👾 Pick, Exclude를 사용하여 Omit구현
type A = Exclude<keyof Profile,'married'>
type O<T, S extends keyof any> = Pick<T, Exclude<keyof T, S>>
const user:O<Profile, 'married'> = {
name: "jiwoo",
age: 24
}
👉🏻 S extends keyof any
: S로 key인 타입만 지정하기 위해 제한조건 추가
type Exclude<T, U> = T extends U ? never : T;
type Animal = 'Cat' | 'Dog' | 'Human';
type Mammal = Exclude<Animal, 'Human'>; // 'Cat' | 'Dog'
interface Profile {
name: string;
age: number;
married: boolean;
}
type A = Exclude<keyof Profile,'married'>
/*
type A = 'name' | 'age'
*/
type Extract<T, U> = T extends U ? T : never;
type Animal = 'Cat' | 'Dog' | 'Human';
type Human = Extract<Animal, 'Human'>; // 'Human'
type A = Extract<keyof Profile,'married'>
/*
type A = 'married'
*/
interface Profile {
name?: string;
age?: number;
married?: boolean;
}
const zooyho: Required<Profile> = {
name: "jiwoo",
age: 24,
married: false,
};
interface Profile {
name?: string;
age?: number;
married?: boolean;
}
const zooyho: Readonly<Profile> = {
name: "jiwoo",
age: 24,
married: false,
};
zooyho.name = "mark"; // error
interface Profile {
readonly name?: string;
readonly age?: number;
readonly married?: boolean;
}
type R<T> = {
-readonly [key in keyof T]-? : T[key];
}
const zooyho: R<Profile> = {
name: "jiwoo",
age: 24,
married: false,
};
zooyho.age = 25;
👾 Record적용 전
interface Obj {
[key: string]: number;
}
const a: Obj = { a: 3, b: 5, c: 7 };
👾 Record적용 후
const a: Record<string, number> = { a: 3, b: 5, c: 7 };
type A = string | null | undefined | number;
type B = NonNullable<A>; // string | number
type A = string | null | undefined | number;
type N<T> = T extends null | undefined ? never : T;
(inference)
function zip(x: string, y: number, z: boolean): {x: string, y: number, z: boolean} {
return {x,y,z};
}
type Fn<T extends (...args: any)=> any> = T extends (...args: infer A ) => any ? A : never;
type Params = Fn<typeof zip>;
function zip(
x: string,
y: number,
z: boolean
): { x: string, y: number, z: boolean } {
return { x, y, z };
}
type Params = ReturnType<typeof zip>; // {x: string, y: number, z: boolean}
타입스크립트 올인원 : Part1. 기본 문법편 강의를 요약 정리하였습니다.