Skip to content

error TS2304 for type definitions with generic assignment #7578

Closed
@0815fox

Description

@0815fox

For:

class Foo<T> {

}

let x = new Foo();

type Y = Foo<string>;

let y = new Y();

tsc complains:

test.ts(9,13): error TS2304: Cannot find name 'Y'.

I would expect the code to be valid. If it were not valid - for which ever reason - then it should be line 7 which it complains about.
Btw. Seems to make no difference, whether i assign T to a literal type like string, or another class.
Of course, as workaround this also works:

class Y extends Foo<string> {};

However, this generates unnecessary runtime code. (e.g. ES5):

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Foo = (function () {
    function Foo() {
    }
    return Foo;
}());
var x = new Foo();
var Y = (function (_super) {
    __extends(Y, _super);
    function Y() {
        _super.apply(this, arguments);
    }
    return Y;
}(Foo));
;
var y = new Y();

Not sure, whether it would be valid to treat empty subclasses like an alias instead of prototypic inheritance.
Also interesting the free floating semi-colon right at the end.

$ tsc -v
Version 1.8.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions