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
Paul-Lo opened this issue
May 27, 2015
· 2 comments
Labels
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
class GenericPractice<T>{
private entity: T;
constructor(entity: T) {
this.entity = entity;
}
public add(item: T): string {
return item.toString();
}
}
class CouponInfo {
public toString(): string {
return "couponInfo";
}
}
class Snake{
}
var genericPractice = new GenericPractice<CouponInfo>(new CouponInfo());
genericPractice.add(new Snake("Sammy the Python")));
theoretically, the above last line:
genericPractice.add(new Snake("Sammy the Python")));
should have compile time error since the generic of class GenericPractice should only allow CouponInfo in method add, not Snake.
However, the compile still passes which violates the fundamental concept of generic.
is this a bug?
The text was updated successfully, but these errors were encountered:
DanielRosenwasser
added
By Design
Deprecated - use "Working as Intended" or "Design Limitation" instead
Canonical
This issue contains a lengthy and complete description of a particular problem, solution, or design
labels
May 27, 2015
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();
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
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
consider the following class.
theoretically, the above last line:
should have compile time error since the generic of class GenericPractice should only allow CouponInfo in method add, not Snake.
However, the compile still passes which violates the fundamental concept of generic.
is this a bug?
The text was updated successfully, but these errors were encountered: