Don't report an error when accessing a name of a global block-scoped variable on `globalThis` outside of `noImplicitAny`

This commit is contained in:
Mateusz Burzyński 2024-06-24 09:09:10 +02:00
Родитель 5d70bf894e
Коммит c6e4b07bf3
20 изменённых файлов: 167 добавлений и 290 удалений

Просмотреть файл

@ -33948,11 +33948,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return anyType;
}
if (leftType.symbol === globalThisSymbol) {
if (globalThisSymbol.exports!.has(right.escapedText) && (globalThisSymbol.exports!.get(right.escapedText)!.flags & SymbolFlags.BlockScoped)) {
error(right, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(right.escapedText), typeToString(leftType));
}
else if (noImplicitAny) {
error(right, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(leftType));
if (noImplicitAny) {
if (globalThisSymbol.exports!.has(right.escapedText) && (globalThisSymbol.exports!.get(right.escapedText)!.flags & SymbolFlags.BlockScoped)) {
error(right, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(right.escapedText), typeToString(leftType));
}
else {
error(right, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(leftType));
}
}
return anyType;
}

Просмотреть файл

@ -1,20 +0,0 @@
emitArrowFunctionThisCapturing.ts(6,10): error TS2339: Property 'name' does not exist on type 'typeof globalThis'.
==== emitArrowFunctionThisCapturing.ts (1 errors) ====
var f1 = () => {
this.age = 10
};
var f2 = (x: string) => {
this.name = x
~~~~
!!! error TS2339: Property 'name' does not exist on type 'typeof globalThis'.
}
function foo(func: () => boolean) { }
foo(() => {
this.age = 100;
return true;
});

Просмотреть файл

@ -11,7 +11,6 @@ var f1 = () => {
>this.age = 10 : 10
> : ^^
>this.age : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>age : any
@ -33,7 +32,6 @@ var f2 = (x: string) => {
>this.name = x : string
> : ^^^^^^
>this.name : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>name : any
@ -60,7 +58,6 @@ foo(() => {
>this.age = 100 : 100
> : ^^^
>this.age : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>age : any

Просмотреть файл

@ -1,20 +0,0 @@
emitArrowFunctionThisCapturingES6.ts(6,10): error TS2339: Property 'name' does not exist on type 'typeof globalThis'.
==== emitArrowFunctionThisCapturingES6.ts (1 errors) ====
var f1 = () => {
this.age = 10
};
var f2 = (x: string) => {
this.name = x
~~~~
!!! error TS2339: Property 'name' does not exist on type 'typeof globalThis'.
}
function foo(func: () => boolean){ }
foo(() => {
this.age = 100;
return true;
});

Просмотреть файл

@ -11,7 +11,6 @@ var f1 = () => {
>this.age = 10 : 10
> : ^^
>this.age : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>age : any
@ -33,7 +32,6 @@ var f2 = (x: string) => {
>this.name = x : string
> : ^^^^^^
>this.name : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>name : any
@ -60,7 +58,6 @@ foo(() => {
>this.age = 100 : 100
> : ^^^
>this.age : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>age : any

Просмотреть файл

@ -0,0 +1,15 @@
//// [tests/cases/conformance/es2019/globalThisBlockscopedProperties2.ts] ////
//// [globalThisBlockscopedProperties2.ts]
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
let y = globalThis.invalid1;
let z = globalThis.invalid2;
//// [globalThisBlockscopedProperties2.js]
// https://github.com/microsoft/TypeScript/issues/58345
var invalid1 = globalThis.invalid1;
var y = globalThis.invalid1;
var z = globalThis.invalid2;

Просмотреть файл

@ -0,0 +1,17 @@
//// [tests/cases/conformance/es2019/globalThisBlockscopedProperties2.ts] ////
=== globalThisBlockscopedProperties2.ts ===
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
>invalid1 : Symbol(invalid1, Decl(globalThisBlockscopedProperties2.ts, 2, 3))
>globalThis : Symbol(globalThis)
let y = globalThis.invalid1;
>y : Symbol(y, Decl(globalThisBlockscopedProperties2.ts, 3, 3))
>globalThis : Symbol(globalThis)
let z = globalThis.invalid2;
>z : Symbol(z, Decl(globalThisBlockscopedProperties2.ts, 4, 3))
>globalThis : Symbol(globalThis)

Просмотреть файл

@ -0,0 +1,29 @@
//// [tests/cases/conformance/es2019/globalThisBlockscopedProperties2.ts] ////
=== globalThisBlockscopedProperties2.ts ===
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
>invalid1 : any
>globalThis.invalid1 : any
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>invalid1 : any
> : ^^^
let y = globalThis.invalid1;
>y : any
>globalThis.invalid1 : any
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>invalid1 : any
> : ^^^
let z = globalThis.invalid2;
>z : any
>globalThis.invalid2 : any
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>invalid2 : any
> : ^^^

Просмотреть файл

@ -0,0 +1,18 @@
globalThisBlockscopedProperties2.ts(3,27): error TS2339: Property 'invalid1' does not exist on type 'typeof globalThis'.
globalThisBlockscopedProperties2.ts(4,20): error TS2339: Property 'invalid1' does not exist on type 'typeof globalThis'.
globalThisBlockscopedProperties2.ts(5,20): error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
==== globalThisBlockscopedProperties2.ts (3 errors) ====
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
~~~~~~~~
!!! error TS2339: Property 'invalid1' does not exist on type 'typeof globalThis'.
let y = globalThis.invalid1;
~~~~~~~~
!!! error TS2339: Property 'invalid1' does not exist on type 'typeof globalThis'.
let z = globalThis.invalid2;
~~~~~~~~
!!! error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

Просмотреть файл

@ -0,0 +1,15 @@
//// [tests/cases/conformance/es2019/globalThisBlockscopedProperties2.ts] ////
//// [globalThisBlockscopedProperties2.ts]
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
let y = globalThis.invalid1;
let z = globalThis.invalid2;
//// [globalThisBlockscopedProperties2.js]
// https://github.com/microsoft/TypeScript/issues/58345
var invalid1 = globalThis.invalid1;
var y = globalThis.invalid1;
var z = globalThis.invalid2;

Просмотреть файл

@ -0,0 +1,17 @@
//// [tests/cases/conformance/es2019/globalThisBlockscopedProperties2.ts] ////
=== globalThisBlockscopedProperties2.ts ===
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
>invalid1 : Symbol(invalid1, Decl(globalThisBlockscopedProperties2.ts, 2, 3))
>globalThis : Symbol(globalThis)
let y = globalThis.invalid1;
>y : Symbol(y, Decl(globalThisBlockscopedProperties2.ts, 3, 3))
>globalThis : Symbol(globalThis)
let z = globalThis.invalid2;
>z : Symbol(z, Decl(globalThisBlockscopedProperties2.ts, 4, 3))
>globalThis : Symbol(globalThis)

Просмотреть файл

@ -0,0 +1,35 @@
//// [tests/cases/conformance/es2019/globalThisBlockscopedProperties2.ts] ////
=== globalThisBlockscopedProperties2.ts ===
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
>invalid1 : any
> : ^^^
>globalThis.invalid1 : any
> : ^^^
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>invalid1 : any
> : ^^^
let y = globalThis.invalid1;
>y : any
> : ^^^
>globalThis.invalid1 : any
> : ^^^
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>invalid1 : any
> : ^^^
let z = globalThis.invalid2;
>z : any
> : ^^^
>globalThis.invalid2 : any
> : ^^^
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>invalid2 : any
> : ^^^

Просмотреть файл

@ -1,15 +1,12 @@
globalThisReadonlyProperties.ts(1,12): error TS2540: Cannot assign to 'globalThis' because it is a read-only property.
globalThisReadonlyProperties.ts(5,12): error TS2339: Property 'y' does not exist on type 'typeof globalThis'.
==== globalThisReadonlyProperties.ts (2 errors) ====
==== globalThisReadonlyProperties.ts (1 errors) ====
globalThis.globalThis = 1 as any // should error
~~~~~~~~~~
!!! error TS2540: Cannot assign to 'globalThis' because it is a read-only property.
var x = 1
const y = 2
globalThis.x = 3
globalThis.y = 4 // should error
~
!!! error TS2339: Property 'y' does not exist on type 'typeof globalThis'.
globalThis.y = 4

Просмотреть файл

@ -5,7 +5,7 @@ globalThis.globalThis = 1 as any // should error
var x = 1
const y = 2
globalThis.x = 3
globalThis.y = 4 // should error
globalThis.y = 4
//// [globalThisReadonlyProperties.js]
@ -13,4 +13,4 @@ globalThis.globalThis = 1; // should error
var x = 1;
var y = 2;
globalThis.x = 3;
globalThis.y = 4; // should error
globalThis.y = 4;

Просмотреть файл

@ -17,6 +17,6 @@ globalThis.x = 3
>globalThis : Symbol(globalThis)
>x : Symbol(x, Decl(globalThisReadonlyProperties.ts, 1, 3))
globalThis.y = 4 // should error
globalThis.y = 4
>globalThis : Symbol(globalThis)

Просмотреть файл

@ -39,7 +39,7 @@ globalThis.x = 3
>3 : 3
> : ^
globalThis.y = 4 // should error
globalThis.y = 4
>globalThis.y = 4 : 4
> : ^
>globalThis.y : any

Просмотреть файл

@ -1,206 +0,0 @@
thisTypeInFunctions.ts(118,29): error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
thisTypeInFunctions.ts(119,32): error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
thisTypeInFunctions.ts(120,36): error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
==== thisTypeInFunctions.ts (3 errors) ====
// body checking
class B {
n: number;
}
class C {
n: number;
explicitThis(this: this, m: number): number {
return this.n + m;
}
explicitC(this: C, m: number): number {
return this.n + m;
}
explicitProperty(this: {n: number}, m: number): number {
return this.n + m;
}
explicitVoid(this: void, m: number): number {
return m + 1;
}
}
class D extends C { }
interface I {
a: number;
explicitVoid1(this: void): number;
explicitVoid2(this: void): number;
explicitStructural(this: {a: number}): number;
explicitInterface(this: I): number;
explicitThis(this: this): number;
}
function explicitStructural(this: { y: number }, x: number): number {
return x + this.y;
}
function justThis(this: { y: number }): number {
return this.y;
}
function implicitThis(n: number): number {
return this.m + n + 12;
}
let impl: I = {
a: 12,
explicitVoid2: () => this.a, // ok, this: any because it refers to some outer object (window?)
explicitVoid1() { return 12; },
explicitStructural() {
return this.a;
},
explicitInterface() {
return this.a;
},
explicitThis() {
return this.a;
},
}
impl.explicitVoid1 = function () { return 12; };
impl.explicitVoid2 = () => 12;
impl.explicitStructural = function() { return this.a; };
impl.explicitInterface = function() { return this.a; };
impl.explicitStructural = () => 12;
impl.explicitInterface = () => 12;
impl.explicitThis = function () { return this.a; };
// parameter checking
let ok: {y: number, f: (this: { y: number }, x: number) => number} = { y: 12, f: explicitStructural };
let implicitAnyOk: {notSpecified: number, f: (x: number) => number} = { notSpecified: 12, f: implicitThis };
ok.f(13);
implicitThis(12);
implicitAnyOk.f(12);
let c = new C();
let d = new D();
let ripped = c.explicitC;
c.explicitC(12);
c.explicitProperty(12);
c.explicitThis(12);
d.explicitC(12);
d.explicitProperty(12);
d.explicitThis(12);
let reconstructed: {
n: number,
explicitThis(this: C, m: number): number, // note: this: this is not allowed in an object literal type.
explicitC(this: C, m: number): number,
explicitProperty: (this: {n : number}, m: number) => number,
explicitVoid(this: void, m: number): number,
} = {
n: 12,
explicitThis: c.explicitThis,
explicitC: c.explicitC,
explicitProperty: c.explicitProperty,
explicitVoid: c.explicitVoid
};
reconstructed.explicitThis(10);
reconstructed.explicitProperty(11);
let explicitVoid = reconstructed.explicitVoid;
explicitVoid(12);
// assignment checking
let unboundToSpecified: (this: { y: number }, x: number) => number = x => x + this.y; // ok, this:any
let specifiedToSpecified: (this: {y: number}, x: number) => number = explicitStructural;
let anyToSpecified: (this: { y: number }, x: number) => number = function(x: number): number { return x + 12; };
let unspecifiedLambda: (x: number) => number = x => x + 12;
let specifiedLambda: (this: void, x: number) => number = x => x + 12;
let unspecifiedLambdaToSpecified: (this: {y: number}, x: number) => number = unspecifiedLambda;
let specifiedLambdaToSpecified: (this: {y: number}, x: number) => number = specifiedLambda;
let explicitCFunction: (this: C, m: number) => number;
let explicitPropertyFunction: (this: {n: number}, m: number) => number;
c.explicitC = explicitCFunction;
c.explicitC = function(this: C, m: number) { return this.n + m };
c.explicitProperty = explicitPropertyFunction;
c.explicitProperty = function(this: {n: number}, m: number) { return this.n + m };
c.explicitProperty = reconstructed.explicitProperty;
// lambdas are assignable to anything
c.explicitC = m => m;
c.explicitThis = m => m;
c.explicitProperty = m => m;
// this inside lambdas refer to outer scope
// the outer-scoped lambda at top-level is still just `any`
c.explicitC = m => m + this.n;
~
!!! error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
c.explicitThis = m => m + this.n;
~
!!! error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
c.explicitProperty = m => m + this.n;
~
!!! error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
//NOTE: this=C here, I guess?
c.explicitThis = explicitCFunction;
c.explicitThis = function(this: C, m: number) { return this.n + m };
// this:any compatibility
c.explicitC = function(m) { return this.n + m };
c.explicitProperty = function(m) { return this.n + m };
c.explicitThis = function(m) { return this.n + m };
// this: contextual typing
c.explicitThis = function(this, m) { return this.n + m };
// this: superclass compatibility
c.explicitC = function(this: B, m: number) { return this.n + m };
// this:void compatibility
c.explicitVoid = n => n;
// class-based assignability
class Base1 {
x: number;
public polymorphic(this: this): number { return this.x; }
explicit(this: Base1): number { return this.x; }
static explicitStatic(this: typeof Base1): number { return this.y; }
static y: number;
}
class Derived1 extends Base1 {
y: number
}
class Base2 {
y: number
polymorphic(this: this): number { return this.y; }
explicit(this: Base1): number { return this.x; }
}
class Derived2 extends Base2 {
x: number
}
let b1 = new Base1();
let b2 = new Base2();
let d1 = new Derived1();
let d2 = new Derived2();
d2.polymorphic = d1.polymorphic // ok, 'x' and 'y' in { x, y }
d1.polymorphic = d2.polymorphic // ok, 'x' and 'y' in { x, y }
// bivariance-allowed cases
d1.polymorphic = b2.polymorphic // ok, 'y' in D: { x, y }
d2.polymorphic = d1.explicit // ok, 'y' in { x, y }
b1.polymorphic = d2.polymorphic // ok, 'x' and 'y' not in Base1: { x }
b1.explicit = d2.polymorphic // ok, 'x' and 'y' not in Base1: { x }
////// use this-type for construction with new ////
function InterfaceThis(this: I) {
this.a = 12;
}
function LiteralTypeThis(this: {x: string}) {
this.x = "ok";
}
function AnyThis(this: any) {
this.x = "ok";
}
let interfaceThis = new InterfaceThis();
let literalTypeThis = new LiteralTypeThis();
let anyThis = new AnyThis();
//// type parameter inference ////
declare var f: {
(this: void, x: number): number,
call<U>(this: (...argArray: any[]) => U, ...argArray: any[]): U;
};
let n: number = f.call(12);
function missingTypeIsImplicitAny(this, a: number) { return this.anything + a; }

Просмотреть файл

@ -186,11 +186,8 @@ function implicitThis(n: number): number {
return this.m + n + 12;
>this.m + n + 12 : any
> : ^^^
>this.m + n : any
> : ^^^
>this.m : any
> : ^^^
>this : any
> : ^^^
>m : any
@ -218,7 +215,6 @@ let impl: I = {
>() => this.a : () => any
> : ^^^^^^^^^
>this.a : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>a : any
@ -705,11 +701,9 @@ let unboundToSpecified: (this: { y: number }, x: number) => number = x => x + th
>x : number
> : ^^^^^^
>x + this.y : any
> : ^^^
>x : number
> : ^^^^^^
>this.y : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>y : any
@ -983,11 +977,9 @@ c.explicitC = m => m + this.n;
>m : number
> : ^^^^^^
>m + this.n : any
> : ^^^
>m : number
> : ^^^^^^
>this.n : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>n : any
@ -1007,11 +999,9 @@ c.explicitThis = m => m + this.n;
>m : number
> : ^^^^^^
>m + this.n : any
> : ^^^
>m : number
> : ^^^^^^
>this.n : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>n : any
@ -1031,11 +1021,9 @@ c.explicitProperty = m => m + this.n;
>m : number
> : ^^^^^^
>m + this.n : any
> : ^^^
>m : number
> : ^^^^^^
>this.n : any
> : ^^^
>this : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>n : any
@ -1499,13 +1487,11 @@ function AnyThis(this: any) {
>AnyThis : (this: any) => void
> : ^ ^^ ^^^^^^^^^
>this : any
> : ^^^
this.x = "ok";
>this.x = "ok" : "ok"
> : ^^^^
>this.x : any
> : ^^^
>this : any
> : ^^^
>x : any
@ -1515,25 +1501,19 @@ function AnyThis(this: any) {
}
let interfaceThis = new InterfaceThis();
>interfaceThis : any
> : ^^^
>new InterfaceThis() : any
> : ^^^
>InterfaceThis : (this: I) => void
> : ^ ^^ ^^^^^^^^^
let literalTypeThis = new LiteralTypeThis();
>literalTypeThis : any
> : ^^^
>new LiteralTypeThis() : any
> : ^^^
>LiteralTypeThis : (this: { x: string; }) => void
> : ^ ^^ ^^^^^^^^^
let anyThis = new AnyThis();
>anyThis : any
> : ^^^
>new AnyThis() : any
> : ^^^
>AnyThis : (this: any) => void
> : ^ ^^ ^^^^^^^^^
@ -1577,13 +1557,10 @@ function missingTypeIsImplicitAny(this, a: number) { return this.anything + a; }
>missingTypeIsImplicitAny : (this: any, a: number) => any
> : ^ ^^^^^^^ ^^ ^^^^^^^^
>this : any
> : ^^^
>a : number
> : ^^^^^^
>this.anything + a : any
> : ^^^
>this.anything : any
> : ^^^
>this : any
> : ^^^
>anything : any

Просмотреть файл

@ -0,0 +1,7 @@
// @noImplicitAny: true, false
// https://github.com/microsoft/TypeScript/issues/58345
let invalid1 = globalThis.invalid1;
let y = globalThis.invalid1;
let z = globalThis.invalid2;

Просмотреть файл

@ -2,4 +2,4 @@ globalThis.globalThis = 1 as any // should error
var x = 1
const y = 2
globalThis.x = 3
globalThis.y = 4 // should error
globalThis.y = 4