Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12602,7 +12602,7 @@ namespace ts {
}
}
}
if (noImplicitThis) {
if (noImplicitThis || isInJavaScriptFile(func)) {
const containingLiteral = getContainingObjectLiteral(func);
if (containingLiteral) {
// We have an object literal method. Check if the containing object literal has a contextual type
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/contextualThisTypeInJavascript.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/conformance/types/thisType/context.js ===
const obj = {
>obj : Symbol(obj, Decl(context.js, 0, 5))

prop: 2,
>prop : Symbol(prop, Decl(context.js, 0, 13))

method() {
>method : Symbol(method, Decl(context.js, 1, 12))

this;
>this : Symbol(obj, Decl(context.js, 0, 11))

this.prop;
>this.prop : Symbol(prop, Decl(context.js, 0, 13))
>this : Symbol(obj, Decl(context.js, 0, 11))
>prop : Symbol(prop, Decl(context.js, 0, 13))

this.method;
>this.method : Symbol(method, Decl(context.js, 1, 12))
>this : Symbol(obj, Decl(context.js, 0, 11))
>method : Symbol(method, Decl(context.js, 1, 12))

this.unknown; // ok, obj has a string indexer
>this : Symbol(obj, Decl(context.js, 0, 11))
}
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/contextualThisTypeInJavascript.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/types/thisType/context.js ===
const obj = {
>obj : { [x: string]: any; prop: number; method(): void; }
>{ prop: 2, method() { this; this.prop; this.method; this.unknown; // ok, obj has a string indexer }} : { [x: string]: any; prop: number; method(): void; }

prop: 2,
>prop : number
>2 : 2

method() {
>method : () => void

this;
>this : { [x: string]: any; prop: number; method(): void; }

this.prop;
>this.prop : number
>this : { [x: string]: any; prop: number; method(): void; }
>prop : number

this.method;
>this.method : () => void
>this : { [x: string]: any; prop: number; method(): void; }
>method : () => void

this.unknown; // ok, obj has a string indexer
>this.unknown : any
>this : { [x: string]: any; prop: number; method(): void; }
>unknown : any
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @Filename: context.js
const obj = {
prop: 2,
method() {
this;
this.prop;
this.method;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for unknown property. Should be no error, get any.

this.unknown; // ok, obj has a string indexer
}
}