Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit utility doesn't respect methods on a type #59357

Closed
d-ellis opened this issue Jul 19, 2024 · 2 comments
Closed

Omit utility doesn't respect methods on a type #59357

d-ellis opened this issue Jul 19, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@d-ellis
Copy link

d-ellis commented Jul 19, 2024

πŸ”Ž Search Terms

Omit, Method

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Omit, Excludes, Methods" (And did a Ctrl+F search for "(): ")

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.3#code/CYUwxgNghgTiAEkoGdnwCImQFxgewE8oAjCBAbwFgAoeO+ABwFdSBLMeUHfAkYALnjE8eMlAB2Abhr1GLCO05ZchABQBKQQDc8rYNOoBfGqCRxEecTngAhFCADC0VIKq164kAHd4GwZm5CEjIDWQZ8bDxsAgYQf2UeYJADQwMTcGhzJFR4ADEoMEiYAng3MPlFHChsRVAAM1ZPAB4nFGQHSwaAc3gQAA9sEHFgNAAlcDwYYCbuRq6AGngJAgA+FdVwqKiYuPhW1A7xbs1SmVkPb18T1QBJK2wJMBAAFR2m6Ni8Ott7feQV+AAMngAHkALasbAtZztTqsBbwADkYEs3CYhUmiJW6lC9FSNGM1BoKPu8AAMiAugUCHZkI4YfAALx5ApFAgAOnqjRAqjK9BJaIxMA0pXghnmZzoyDwYJAAAV8LEYNFBABGABMAGYJe4pTLHFAIBBiAUANaCEWMgHiJhGnWyaWygCyIGwAAs8MA-PAdHpTrrzgB6QMYPDwADK+vdc0lYoJOJoxIZTs9IBg4j+vQGQxG5Mp1Np9La-vKbA4eC0aZgegQjpALvdnu9vuAJfOdGDscJhiAA

πŸ’» Code

declare class Destroyable {
    public destroyed: boolean;
    public destroy(): void;
}
declare const BaseClass: {
    new (): Destroyable;
    prototype: Destroyable;
};

declare class Factory {
    public static define<ClassConfig extends Record<string, any>>(prototype: ClassConfig): {
        new (): (InstanceType<typeof BaseClass> & Omit<ClassConfig, 'constructor'>);
    };
}

const LegacyBaseClass = Factory.define({
    constructor() { },
    someProperty: 123,
    someCallback: () => null,
    someMethod(): void {
        // Do Something
    }
});

class ModernClass extends LegacyBaseClass {
    public override someMethod(): void {
        //
    }
}

πŸ™ Actual behavior

ModernClass cannot override someMethod because of the following error
Class 'Destroyable & Omit<{ someProperty: number; someCallback: () => null; someMethod(): void; }, "constructor">' defines instance member property 'someMethod', but extended class 'ModernClass' defines it as instance member function.

πŸ™‚ Expected behavior

ModernClass should be able to add an override because someMethod is defined as a method not a property

Additional information about the issue

The issue is coming from the fact that Omit is not respecting the difference between methods and properties on the type passed into it.

type ClassConfig = {
    constructor(): any;
    someProperty: number;
    someCallback: () => null;
    someMethod(): void;
};

type RelevantConfig = Omit<ClassConfig, 'constructor'>;

// Expected
type RelevantConfig = {
    someProperty: number;
    someCallback: () => null;
    someMethod(): void;
};

// Actual
type RelevantConfig = {
    someProperty: number;
    someCallback: () => null;
    someMethod: () => void;
};
@jcalz
Copy link
Contributor

jcalz commented Jul 19, 2024

Duplicate of #27689

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants