You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceIObj{doSmth(): void;}classObjimplementsIObj{publicdoSmth(){console.log('smth');}}consto: IObj=newObj();constclone={ ...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
consto=newObj();
instead of
consto: IObj=newObj();
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"?
The text was updated successfully, but these errors were encountered:
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
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.
Bug Report
π Search Terms
interface inherited property
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
no tsc compiler error
π Expected behavior
I expect
const o: IObj = new Obj();
to generate an error becausedoSmth
is not an own enumerable property ofo
(it is inherited from the prototype). The spread operator seems to assume thatdoSmth
is an own property ofo
, so, as a result, clone's type is{ doSmth(): void; }
but the "actual" type is{}
.If we have
instead of
we get a
Property 'doSmth' does not exist on type '{}'
error which indicates thatIObj
andObj
are clearly not compatible.Perhaps we should be able to mark
doSmth(): void;
as "inherited"?The text was updated successfully, but these errors were encountered: