Home > Blockchain >  The issue with using of Omit for generic type in TypeScript
The issue with using of Omit for generic type in TypeScript

Time:05-08

There is the minimum reproducible example:

interface BasicObject {
    name: string;
}

interface Object extends BasicObject {
    code: string
}

function generic<T extends BasicObject> (options: T) {
    // something
}

function externalGeneric<T extends BasicObject>(a: Omit<T, 'name'>) {
    generic(a);
}

Playground.

Could you explain, why Typescript ignores the fact that a doesn't have name field and therefore doesn't extend BasicObject?

The version of Typescript is 4.6.2.

CodePudding user response:

Because you redeclare global Object interface, try change

interface Object extends BasicObject {
    code: string
}

To

interface SomeOtherNameThanObject extends BasicObject {
    code: string
}
  • Related