Skip to content

is this a Generic bug? #3275

Closed
Closed
@Paul-Lo

Description

@Paul-Lo

consider the following class.

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?

Metadata

Metadata

Assignees

No one assigned

    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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions