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
interfaceIGreeter{greeting: stringgreet(): string}classGreeterimplementsIGreeter{publicconstructor(publicgreeting: string){}greet(){returnthis.greeting;}// Runtime error if greet() is called. Objects will not have a greet() method. (Unexpected)publicstaticgreeterFactory1(greeting: string): IGreeter{return{
...this.emptyGreeter(),
greeting
}}// // Compile error. Objects will not have a greet() method.// public static greeterFactory2(greeting: string): IGreeter {// return {// ...new this(""),// greeting // }// }publicstaticemptyGreeter(): IGreeter{returnnewthis("");}}
Expected behavior:
I would expect the above code to not compile for the following reason:
Type '{ greeting: string; }' is not assignable to type 'IGreeter'.
Property 'greet' is missing in type '{ greeting: string; }'.
This is the behavior when uncommented greeterFactory2.
Actual behavior:
Compilation succeeds and runtime exceptions will be thrown when greet() is called.
However, both interact differently with the spread operator.
There is not currently a way to express the ownness of a property at the type level and therefore no way to explicitly express the type level transformation implied by the spread operator.
However, the language does understand the distinction and tracks it correctly if you let it do its job.
The types of objects created by spreading other objects are in fact tracked and enforced.
However, by specifying the return type of emptyGreeter as IGreeter you have removed type information that the compiler would otherwise have, information it needs to track the effects of spreading an object. If you remove the type annotation, you will get the expected errors.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.8.0-dev.20180127
Search Terms:
instance method spread static factory
Code
Expected behavior:
I would expect the above code to not compile for the following reason:
This is the behavior when uncommented greeterFactory2.
Actual behavior:
Compilation succeeds and runtime exceptions will be thrown when greet() is called.
Playground Link: Playground Link
The text was updated successfully, but these errors were encountered: