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
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
The text was updated successfully, but these errors were encountered:
Types do not exist at runtime, they are just design time constructs that help guide the compiler to understand your code. For instance interfaces have no runtime manifestation. type Y = Foo<string>; is a type alias; and thus it is just like an interface. the new expression in new Y(); assumes a value called Y exists at runtime, but that is not the case.
A class is really an interface (instance type) and a variable (constructor function), so you can achieve this by spiting your definition into two. There is still a missing piece though, which is trafficking the generic type between he constructor and the instance correctly. for that the feature request is tracked in #2559
Well, I have to accept that.
Still disagree with your argument - it just explains why it is as it is, not why is has to be like that.
Alias is for me a placeholder. So when one defines Frank as an alias for Mohamed, then he'd expect Frank to work just like Mohamed. But you are right, under the hood there is no Frank, so the compiler has to be smart enough to keep track of aliases and when the programmer writes "new Y()", it should see: ah Y is an alias for X, so i generate "new X()" instead.
For:
tsc complains:
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:
However, this generates unnecessary runtime code. (e.g. ES5):
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.
The text was updated successfully, but these errors were encountered: