allow this in typeQuery (#43898)

* allow `this` in typeQuery

* add tests

* get this type as expression

* handle nested nodes

* update baselines
This commit is contained in:
Zzzen
2021-06-17 22:52:20 +08:00
committed by GitHub
parent d46d82c9bd
commit 8dbb2cd5fb
17 changed files with 1282 additions and 21 deletions

View File

@@ -14,7 +14,7 @@ class D {
class E {
a = this.x; // ok
b: typeof this.x; // error
b: typeof this.x; // ok
constructor(public x) { }
}

View File

@@ -0,0 +1,125 @@
// @noImplicitThis: true
// @strict: true
class Test {
data = {};
constructor() {
var copy: typeof this.data = {};
}
}
class Test1 {
data = { foo: '' };
['this'] = '';
constructor() {
var copy: typeof this.data = { foo: '' };
var foo: typeof this.data.foo = '';
var self: typeof this = this;
self.data;
var str: typeof this.this = '';
}
}
function Test2() {
let x: typeof this.no = 1;
}
function Test3(this: { no: number }) {
let x: typeof this.no = 1;
}
function Test4(this: { no: number } | undefined) {
let x: typeof this.no = 1;
}
class Test5 {
no = 1;
f = () => {
// should not capture this.
let x: typeof this.no = 1;
}
}
namespace Test6 {
export let f = () => {
let x: typeof this.no = 1;
}
}
module Test7 {
export let f = () => {
let x: typeof this.no = 1;
}
}
const Test8 = () => {
let x: typeof this.no = 1;
}
class Test9 {
no = 0;
this = 0;
f() {
if (this instanceof Test9D1) {
const d1: typeof this = this;
d1.f1();
}
if (this instanceof Test9D2) {
const d2: typeof this = this;
d2.f2();
}
}
g() {
if (this.no === 1) {
const no: typeof this.no = this.no;
}
if (this.this === 1) {
const no: typeof this.this = this.this;
}
}
}
class Test9D1 {
f1() {}
}
class Test9D2 {
f2() {}
}
class Test10 {
a?: { b?: string }
foo() {
let a: typeof this.a = undefined as any;
if (this.a) {
let a: typeof this.a = undefined as any; // should narrow to { b?: string }
let b: typeof this.a.b = undefined as any;
if (this.a.b) {
let b: typeof this.a.b = undefined as any; // should narrow to string
}
}
}
}
class Test11 {
this?: { x?: string };
foo() {
const o = this;
let bar: typeof o.this = {};
if (o.this && o.this.x) {
let y: string = o.this.x; // should narrow to string
}
}
}

View File

@@ -0,0 +1,5 @@
// @noImplicitThis: false
function Test1() {
let x: typeof this.no = 1
}