Skip to content

Cannot specify that a property is inherited when defining interface #51073

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

Closed
vasyop opened this issue Oct 5, 2022 · 3 comments
Closed

Cannot specify that a property is inherited when defining interface #51073

vasyop opened this issue Oct 5, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@vasyop
Copy link

vasyop commented Oct 5, 2022

Bug Report

πŸ”Ž Search Terms

interface inherited property

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about interfaces and classes

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface IObj {
    doSmth(): void;
}

class Obj implements IObj {
    public doSmth() {
        console.log('smth');
    }
}

const o: IObj = new Obj();

const clone = { ...o };
clone.doSmth(); // Uncaught TypeError: clone.doSmth is not a function

πŸ™ Actual behavior

no tsc compiler error

πŸ™‚ Expected behavior

I expect const o: IObj = new Obj(); to generate an error because doSmth is not an own enumerable property of o (it is inherited from the prototype). The spread operator seems to assume that doSmth is an own property of o, so, as a result, clone's type is { doSmth(): void; } but the "actual" type is {}.

If we have

const o = new Obj();

instead of

const o: IObj = new Obj();

we get a Property 'doSmth' does not exist on type '{}' error which indicates that IObj and Obj are clearly not compatible.
Perhaps we should be able to mark doSmth(): void; as "inherited"?

@vasyop vasyop changed the title Cannot specify that property is inherited when defining interface Cannot specify that a property is inherited when defining interface Oct 5, 2022
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 5, 2022
@RyanCavanaugh
Copy link
Member

#9726

@vasyop
Copy link
Author

vasyop commented Oct 6, 2022

I ran into this after using the spread operator on a Promise and the later calling .then() on it, which is apparently fine at compile time, but fails at runtime.
TS behaved as if the type of { ...somePromise } is Promise which is simply wrong.
I can imagine easily running into similar issues with other standard built-in objects.

@typescript-bot
Copy link
Collaborator

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

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

3 participants