'Can't extract generic part from the function arguments in Typescript
There is the basic example:
interface BasicProperties {
a: number,
b: number,
c: number
}
interface Properties extends BasicProperties {
d: number
}
function generic<T extends BasicProperties>(args: Omit<T, keyof BasicProperties> & Partial<BasicProperties>): T {
const {
a,
b,
c,
...other
} = args
const properties: Omit<T, keyof BasicProperties>= other;
// The properties is the temporary value. It passed to a complicated function that returns the fully defined T
// Just example:
return {
a: a || 0,
b: b || 0,
c: c || 0,
...properties,
}
}
It leads to the following error:
Type 'Omit<Omit<T, keyof BasicProperties> & Partial, "a" | "b" | "c">' is not assignable to type 'Omit<T, keyof BasicProperties>'. Type 'Exclude<keyof T, keyof BasicProperties>' is not assignable to type 'Exclude<Exclude<keyof T, keyof BasicProperties>, "a" | "b" | "c">'. Type 'keyof T' is not assignable to type 'Exclude<Exclude<keyof T, keyof BasicProperties>, "a" | "b" | "c">'. Type 'string | number | symbol' is not assignable to type 'Exclude<Exclude<keyof T, keyof BasicProperties>, "a" | "b" | "c">'. Type 'string' is not assignable to type 'Exclude<Exclude<keyof T, keyof BasicProperties>, "a" | "b" | "c">'. Type 'keyof T' is not assignable to type 'Exclude<keyof T, keyof BasicProperties>'.
What's wrong with properties type?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
