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
By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadCanonicalThis issue contains a lengthy and complete description of a particular problem, solution, or designQuestionAn issue which isn't directly actionable in code
Note: I assume that you didn't mean to supply an argument to the Snake constructor.
This has to do with TypeScript having a predominantly structural type system. For that reason, the following works:
classCouponInfo{publictoString(): string{return"couponInfo";}}classSnake{}// No problem with either of these.varx: CouponInfo=newSnake();vary: Snake=newCouponInfo();
Since instances of Snake have the same apparent members as those of CouponInfo, Snakes are assignable to CouponInfos.
Thus when add expects a CouponInfo, you can give it a Snake.
If you want to avoid this, you can add a private "tag" member in each class. Privates are not compared in a structural manner, regardless of their name and type.
classCouponInfo{// Introduced a tag here.privatecouponTag: {};publictoString(): string{return"couponInfo";}}classSnake{// Introduced a tag here.privatesnakeTag: {};}vargenericPractice=newGenericPractice(newCouponInfo());// Now all of these cause errors.genericPractice.add(newSnake());varx: CouponInfo=newSnake();vary: Snake=newCouponInfo();
By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadCanonicalThis issue contains a lengthy and complete description of a particular problem, solution, or designQuestionAn issue which isn't directly actionable in code
Activity
DanielRosenwasser commentedon May 27, 2015
Note: I assume that you didn't mean to supply an argument to the
Snakeconstructor.This has to do with TypeScript having a predominantly structural type system. For that reason, the following works:
Since instances of
Snakehave the same apparent members as those ofCouponInfo,Snakes are assignable toCouponInfos.Thus when
addexpects aCouponInfo, you can give it aSnake.If you want to avoid this, you can add a private "tag" member in each class. Privates are not compared in a structural manner, regardless of their name and type.
Hope that answers your question!
Paul-Lo commentedon May 27, 2015
Thanks for the informative and comprehensive answer!