Define an interface like this
interface Props {
a?: string
b?: string
c?: string
d: string
}
When I do something like this, it is ok
type Test1 = {
[P in keyof Props]: Props[P]
}
const a1: Test1 = {
d: '1'
}
but, like this, I got some error message
type Keys = keyof Props;
type Test1 = {
[P in Keys]: Props[P]
}
// error Type '{ d: string; }' is missing the following properties from type 'Test1': a, b, c(2739)
const a1: Test1 = {
d: '1'
}
Another one
type MyOmit<T, K extends keyof any> = { [P in Exclude<keyof T, K>]: T[P]; }
// Property 'a' is missing in type '{ d: string; }' but required in type 'MyOmit<Props, "b" | "c">'.
const o1: MyOmit<Props, 'b' | 'c'> = {
d: '1'
}
// Its Ok!
const o2: Omit<Props, 'b' | 'c'> = {
d: '1'
}
Why MyOmit get error?