mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 04:43:37 -05:00
Contextually type this in object literals in JS
Previously, `this` would only get a contextual type inside object literals with `--noImplicitThis` turned on in Typescript files.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
=== 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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
=== 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; }} : { [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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// @allowJs: true
|
||||
// @checkJs: true
|
||||
// @noEmit: true
|
||||
// @Filename: context.js
|
||||
const obj = {
|
||||
prop: 2,
|
||||
method() {
|
||||
this;
|
||||
this.prop;
|
||||
this.method;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user