TypeScript/tests/cases/conformance/expressions/thisKeyword/typeOfThis.ts
Nathan Shively-Sanders be2db9db12
Add globalThis (#29332)
* Restore original code from bind-toplevel-this

With one or two additional comments

* Working in JS, but the symbol is not right.

Still need to

1. Make it work in Typescript.
2. Add test (and make them work) for the other uses of GlobalThis:
window, globalThis, etc.

* Check in TS also; update some tests

Lots of tests still fail, but all but 1 change so far has been correct.

* Update baselines

A couple of tests still fail and need to be fixed.

* Handle type references to globalThis

The type reference must be `typeof globalThis`. Just `globalThis` will
be treated as a value reference in type position -- an error.

* Restore former behaviour of implicitThis errors

I left the noImplicitThis rule for captured use of global this in an
arrow function, even though technically it isn't `any` any more --
it's typeof globalThis.  However, you should still use some other method
to access globals inside an arrow, because captured-global-this is super
confusing there.

* Test values with type globalThis

I ran into a problem with intersecting `Window & typeof globalThis`:

1. This adds a new index signature to Window, which is probably not
desired. In fact, with noImplicitAny, it's not desired on globalThis
either I think.
2. Adding this type requires editing TSJS-lib-generator, not this repo.

So I added the test cases and will probably update them later, when
those two problems are fixed.

* Add esnext declaration for globalThis

* Switch to symbol-based approach

I decided I didn't like the import-type-based approach.

Update baselines to reflect the difference.

* Do not suggest globals for completions at toplevel

* Add tests of element and property access

* Look up globalThis using normal resolution

globalThis is no longer constructed lazily. Its synthetic Identifier
node is also now more realistic.

* Update fourslash tests

* Add missed fourslash test update

* Remove esnext.globalthis.d.ts too

* Add chained globalThis self-lookup test

* Attempt at making globalThis readonly

In progress, had to interrupt for other work.

* Add/update tests

* Addres PR comments:

1. Add parameter to tryGetThisTypeAt to exclude globalThis.
2. Use combined Module flag instead combining them in-place.
3. SymbolDisplay doesn't print 'module globalThis' for this expressions
anymore.
2019-02-27 14:14:34 -08:00

177 lines
4.9 KiB
TypeScript

// @target: esnext
class MyTestClass {
private canary: number;
static staticCanary: number;
constructor() {
//type of 'this' in constructor body is the class instance type
var p = this.canary;
var p: number;
this.canary = 3;
}
//type of 'this' in member function param list is the class instance type
memberFunc(t = this) {
var t: MyTestClass;
//type of 'this' in member function body is the class instance type
var p = this;
var p: MyTestClass;
}
//type of 'this' in member accessor(get and set) body is the class instance type
get prop() {
var p = this;
var p: MyTestClass;
return this;
}
set prop(v) {
var p = this;
var p: MyTestClass;
p = v;
v = p;
}
someFunc = () => {
//type of 'this' in member variable initializer is the class instance type
var t = this;
var t: MyTestClass;
};
//type of 'this' in static function param list is constructor function type
static staticFn(t = this) {
var t: typeof MyTestClass;
var t = MyTestClass;
t.staticCanary;
//type of 'this' in static function body is constructor function type
var p = this;
var p: typeof MyTestClass;
var p = MyTestClass;
p.staticCanary;
}
static get staticProp() {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyTestClass;
var p = MyTestClass;
p.staticCanary;
return this;
}
static set staticProp(v: typeof MyTestClass) {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyTestClass;
var p = MyTestClass;
p.staticCanary;
}
}
class MyGenericTestClass<T, U> {
private canary: number;
static staticCanary: number;
constructor() {
//type of 'this' in constructor body is the class instance type
var p = this.canary;
var p: number;
this.canary = 3;
}
//type of 'this' in member function param list is the class instance type
memberFunc(t = this) {
var t: MyGenericTestClass<T, U>;
//type of 'this' in member function body is the class instance type
var p = this;
var p: MyGenericTestClass<T, U>;
}
//type of 'this' in member accessor(get and set) body is the class instance type
get prop() {
var p = this;
var p: MyGenericTestClass<T, U>;
return this;
}
set prop(v) {
var p = this;
var p: MyGenericTestClass<T, U>;
p = v;
v = p;
}
someFunc = () => {
//type of 'this' in member variable initializer is the class instance type
var t = this;
var t: MyGenericTestClass<T, U>;
};
//type of 'this' in static function param list is constructor function type
static staticFn(t = this) {
var t: typeof MyGenericTestClass;
var t = MyGenericTestClass;
t.staticCanary;
//type of 'this' in static function body is constructor function type
var p = this;
var p: typeof MyGenericTestClass;
var p = MyGenericTestClass;
p.staticCanary;
}
static get staticProp() {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyGenericTestClass;
var p = MyGenericTestClass;
p.staticCanary;
return this;
}
static set staticProp(v: typeof MyGenericTestClass) {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyGenericTestClass;
var p = MyGenericTestClass;
p.staticCanary;
}
}
//type of 'this' in a function declaration param list is Any
function fn(s = this) {
var s: any;
s.spaaaaaaace = 4;
//type of 'this' in a function declaration body is Any
var t: any;
var t = this;
this.spaaaaace = 4;
}
//type of 'this' in a function expression param list list is Any
var q1 = function (s = this) {
var s: any;
s.spaaaaaaace = 4;
//type of 'this' in a function expression body is Any
var t: any;
var t = this;
this.spaaaaace = 4;
}
//type of 'this' in a fat arrow expression param list is typeof globalThis
var q2 = (s = this) => {
var s: typeof globalThis;
s.spaaaaaaace = 4;
//type of 'this' in a fat arrow expression body is typeof globalThis
var t: typeof globalThis;
var t = this;
this.spaaaaace = 4;
}
//type of 'this' in global module is GlobalThis
var t: typeof globalThis;
var t = this;
this.spaaaaace = 4;