type WeekDay = 'Mon'|'Tue'|'Wed'|'Thu'|'Fri';
type Day = WeekDay|'Sat' |'Sun';
// type Record<K extends keyof any,T>={
// [P in K]:T
// }
const day:Record<WeekDay,Day> = {
Fri:'Fri',
Mon:'Mon',
Thu:'Thu',
Wed:'Wed',
Tue:'Tue'
}
type Account = {
id:number;
isEmploy:boolean;
notes:string[];
}
type ReadonlyAccount = {
readonly [K in keyof Account]:Account[K];
}
type ReadonlyAccount2 = {
-readonly [K in keyof Account]:Account[K];
}
type OptionalAccount = {
[K in keyof Account]:Account[K]
}
type ABC = 'A'|'B'|'C';
type In = {[K in ABC]:K}
const abc0:In = {
A:"A",
B:"B",
C:"C"
}
interface AB {
A:'A',
B:'B'
}
type InKeyOf = {
[K in keyof AB]:K
}
type InKeyOfKeyIn = {
[K in keyof AB]:AB[keyof AB]
}
const abc1:InKeyOf = {
A:"A",
B:"B"
}
const abc2:InKeyOfKeyIn = {
A:"B",
B:"A"
}
const d:OptionalAccount = {
id:1,
isEmploy:true,
notes:['w']
}
d.id=2; // ok
const dd:ReadonlyAccount = {
id:1,
isEmploy:true,
notes:['w']
}
// dd.id = 2; // error!
const ddd:ReadonlyAccount2 = {
id:1,
isEmploy:true,
notes:['w']
}
ddd.id = 2; // ok
type Dig<T, U extends keyof T> = Pick<T, U>[U]
const obj = {
a: {
first: '1',
second: '2',
thrid: '3',
},
b: 'b',
c: {
fourth: '4',
fifth: '5'
}
}
const Dig:Dig<typeof obj,'a'> = {
first:'3',
second:'4',
thrid:'5'
}
type Diff<T, U> = T extends U ? never : T
type DiffExample = Diff<'a' | 'b' | 'c' | 'd', 'a' | 'c' | 'f'>; // 'b' | 'd'
아래의 Dig, Diff는 안도형님의 글을 참고하였습니다
안도형님의 블로그