Skip to content

Commit 12cd15c

Browse files
authored
this: undefined in modules (microsoft#37784)
It's always supposed to have been this way, but I was worried about how breaky the change would be when adding globalThisType. This PR is experiment to see how much. Fixes microsoft#35882 maybe
1 parent 167f954 commit 12cd15c

16 files changed

+72
-22
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21660,6 +21660,10 @@ namespace ts {
2166021660
const fileSymbol = getSymbolOfNode(container);
2166121661
return fileSymbol && getTypeOfSymbol(fileSymbol);
2166221662
}
21663+
else if (container.externalModuleIndicator) {
21664+
// TODO: Maybe issue a better error than 'object is possibly undefined'
21665+
return undefinedType;
21666+
}
2166321667
else if (includeGlobalThis) {
2166421668
return getTypeOfSymbol(globalThisSymbol);
2166521669
}

tests/baselines/reference/inlineJsxFactoryDeclarationsLocalTypes.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
tests/cases/conformance/jsx/inline/component.tsx(4,136): error TS2609: JSX spread child must be an array type.
2+
tests/cases/conformance/jsx/inline/component.tsx(4,140): error TS2532: Object is possibly 'undefined'.
13
tests/cases/conformance/jsx/inline/index.tsx(5,1): error TS2741: Property '__predomBrand' is missing in type 'import("tests/cases/conformance/jsx/inline/renderer").dom.JSX.Element' but required in type 'import("tests/cases/conformance/jsx/inline/renderer2").predom.JSX.Element'.
24
tests/cases/conformance/jsx/inline/index.tsx(21,22): error TS2786: 'MySFC' cannot be used as a JSX component.
35
Its return type 'import("tests/cases/conformance/jsx/inline/renderer2").predom.JSX.Element' is not a valid JSX element.
@@ -53,11 +55,15 @@ tests/cases/conformance/jsx/inline/index.tsx(24,48): error TS2322: Type 'import(
5355
}
5456
}
5557
export function predom(): predom.JSX.Element;
56-
==== tests/cases/conformance/jsx/inline/component.tsx (0 errors) ====
58+
==== tests/cases/conformance/jsx/inline/component.tsx (2 errors) ====
5759
/** @jsx predom */
5860
import { predom } from "./renderer2"
5961

6062
export const MySFC = (props: {x: number, y: number, children?: predom.JSX.Element[]}) => <p>{props.x} + {props.y} = {props.x + props.y}{...this.props.children}</p>;
63+
~~~~~~~~~~~~~~~~~~~~~~~~
64+
!!! error TS2609: JSX spread child must be an array type.
65+
~~~~
66+
!!! error TS2532: Object is possibly 'undefined'.
6167

6268
export class MyClass implements predom.JSX.Element {
6369
__predomBrand!: void;

tests/baselines/reference/inlineJsxFactoryDeclarationsLocalTypes.symbols

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export const MySFC = (props: {x: number, y: number, children?: predom.JSX.Elemen
127127
>props.y : Symbol(y, Decl(component.tsx, 3, 40))
128128
>props : Symbol(props, Decl(component.tsx, 3, 22))
129129
>y : Symbol(y, Decl(component.tsx, 3, 40))
130-
>this : Symbol(globalThis)
131130
>p : Symbol(predom.JSX.IntrinsicElements, Decl(renderer2.d.ts, 1, 19))
132131

133132
export class MyClass implements predom.JSX.Element {

tests/baselines/reference/inlineJsxFactoryDeclarationsLocalTypes.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const MySFC = (props: {x: number, y: number, children?: predom.JSX.Elemen
9999
>y : number
100100
>this.props.children : any
101101
>this.props : any
102-
>this : typeof globalThis
102+
>this : undefined
103103
>props : any
104104
>children : any
105105
>p : any

tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod
33
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(22,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
44
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body.
55
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(36,13): error TS2526: A 'this' type is available only in a non-static member of a class or interface.
6-
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(38,25): error TS2507: Type 'typeof globalThis' is not a constructor function type.
6+
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(38,25): error TS2507: Type 'undefined' is not a constructor function type.
77
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(44,9): error TS2332: 'this' cannot be referenced in current location.
88
tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(45,9): error TS2332: 'this' cannot be referenced in current location.
99

@@ -58,7 +58,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod
5858

5959
class ErrClass3 extends this {
6060
~~~~
61-
!!! error TS2507: Type 'typeof globalThis' is not a constructor function type.
61+
!!! error TS2507: Type 'undefined' is not a constructor function type.
6262

6363
}
6464

tests/baselines/reference/thisInInvalidContextsExternalModule.symbols

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ genericFunc<this>(undefined); // Should be an error
6969

7070
class ErrClass3 extends this {
7171
>ErrClass3 : Symbol(ErrClass3, Decl(thisInInvalidContextsExternalModule.ts, 35, 29))
72-
>this : Symbol(globalThis)
7372

7473
}
7574

tests/baselines/reference/thisInInvalidContextsExternalModule.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ genericFunc<this>(undefined); // Should be an error
7272

7373
class ErrClass3 extends this {
7474
>ErrClass3 : ErrClass3
75-
>this : typeof globalThis
75+
>this : undefined
7676

7777
}
7878

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/compiler/topLevelLambda4.ts(1,22): error TS2532: Object is possibly 'undefined'.
2+
3+
4+
==== tests/cases/compiler/topLevelLambda4.ts (1 errors) ====
5+
export var x = () => this.window;
6+
~~~~
7+
!!! error TS2532: Object is possibly 'undefined'.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
=== tests/cases/compiler/topLevelLambda4.ts ===
22
export var x = () => this.window;
33
>x : Symbol(x, Decl(topLevelLambda4.ts, 0, 10))
4-
>this.window : Symbol(window, Decl(lib.dom.d.ts, --, --))
5-
>this : Symbol(globalThis)
6-
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
74

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
=== tests/cases/compiler/topLevelLambda4.ts ===
22
export var x = () => this.window;
3-
>x : () => Window & typeof globalThis
4-
>() => this.window : () => Window & typeof globalThis
5-
>this.window : Window & typeof globalThis
6-
>this : typeof globalThis
7-
>window : Window & typeof globalThis
3+
>x : () => any
4+
>() => this.window : () => any
5+
>this.window : any
6+
>this : undefined
7+
>window : any
88

0 commit comments

Comments
 (0)