nitrovatter
nitrovatter

Reputation: 1201

The issue with using of Omit for generic type in TypeScript

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.

Upvotes: 2

Views: 50

Answers (1)

Because you redeclare global Object interface, try change

interface Object extends BasicObject {
    code: string
}

To

interface SomeOtherNameThanObject extends BasicObject {
    code: string
}

Upvotes: 2

Related Questions