Illustrate an interface in the following way
interface Properties {
apple?: string
banana?: string
cherry?: string
date: string
}
Executing this code works as expected
type Sample1 = {
[P in keyof Properties]: Properties[P]
}
const s1: Sample1 = {
date: '1'
}
However, when I modify it like below, errors arise
type Keys = keyof Properties;
type Sample2 = {
[P in Keys]: Properties[P]
}
// error Type '{ d: string; }' is missing the following properties from type 'Sample2': apple, banana, cherry(2739)
const s2: Sample2 = {
date: '1'
}
Delving deeper
type CustomOmit<T, K extends keyof any> = { [P in Exclude<keyof T, K>]: T[P]; }
// Property 'apple' is missing in type '{ date: string; }' but required in type 'CustomOmit<Properties, "banana" | "cherry">'.
const o3: CustomOmit<Properties, 'banana' | 'cherry'> = {
date: '1'
}
// No issues here!
const o4: Omit<Properties, 'banana' | 'cherry'> = {
date: '1'
}
Why does CustomOmit trigger an error?