Skip to content

Commit fb89d47

Browse files
authored
Merge pull request #17252 from Microsoft/contextually-type-this-in-object-literals-in-javascript
Contextually type this in object literals in JS
2 parents 068b17a + 95f5bc1 commit fb89d47

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12662,7 +12662,7 @@ namespace ts {
1266212662
}
1266312663
}
1266412664
}
12665-
if (noImplicitThis) {
12665+
if (noImplicitThis || isInJavaScriptFile(func)) {
1266612666
const containingLiteral = getContainingObjectLiteral(func);
1266712667
if (containingLiteral) {
1266812668
// We have an object literal method. Check if the containing object literal has a contextual type
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/conformance/types/thisType/context.js ===
2+
const obj = {
3+
>obj : Symbol(obj, Decl(context.js, 0, 5))
4+
5+
prop: 2,
6+
>prop : Symbol(prop, Decl(context.js, 0, 13))
7+
8+
method() {
9+
>method : Symbol(method, Decl(context.js, 1, 12))
10+
11+
this;
12+
>this : Symbol(obj, Decl(context.js, 0, 11))
13+
14+
this.prop;
15+
>this.prop : Symbol(prop, Decl(context.js, 0, 13))
16+
>this : Symbol(obj, Decl(context.js, 0, 11))
17+
>prop : Symbol(prop, Decl(context.js, 0, 13))
18+
19+
this.method;
20+
>this.method : Symbol(method, Decl(context.js, 1, 12))
21+
>this : Symbol(obj, Decl(context.js, 0, 11))
22+
>method : Symbol(method, Decl(context.js, 1, 12))
23+
24+
this.unknown; // ok, obj has a string indexer
25+
>this : Symbol(obj, Decl(context.js, 0, 11))
26+
}
27+
}
28+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/conformance/types/thisType/context.js ===
2+
const obj = {
3+
>obj : { [x: string]: any; prop: number; method(): void; }
4+
>{ prop: 2, method() { this; this.prop; this.method; this.unknown; // ok, obj has a string indexer }} : { [x: string]: any; prop: number; method(): void; }
5+
6+
prop: 2,
7+
>prop : number
8+
>2 : 2
9+
10+
method() {
11+
>method : () => void
12+
13+
this;
14+
>this : { [x: string]: any; prop: number; method(): void; }
15+
16+
this.prop;
17+
>this.prop : number
18+
>this : { [x: string]: any; prop: number; method(): void; }
19+
>prop : number
20+
21+
this.method;
22+
>this.method : () => void
23+
>this : { [x: string]: any; prop: number; method(): void; }
24+
>method : () => void
25+
26+
this.unknown; // ok, obj has a string indexer
27+
>this.unknown : any
28+
>this : { [x: string]: any; prop: number; method(): void; }
29+
>unknown : any
30+
}
31+
}
32+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
// @Filename: context.js
5+
const obj = {
6+
prop: 2,
7+
method() {
8+
this;
9+
this.prop;
10+
this.method;
11+
this.unknown; // ok, obj has a string indexer
12+
}
13+
}

0 commit comments

Comments
 (0)