From 08ae02263ae8b8c4d11034c3678c733c99561aa3 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 17 Jul 2017 15:59:18 -0700 Subject: [PATCH 1/2] 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. --- src/compiler/checker.ts | 2 +- .../contextualThisTypeInJavascript.symbols | 25 +++++++++++++++++ .../contextualThisTypeInJavascript.types | 27 +++++++++++++++++++ .../contextualThisTypeInJavascript.ts | 12 +++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/contextualThisTypeInJavascript.symbols create mode 100644 tests/baselines/reference/contextualThisTypeInJavascript.types create mode 100644 tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dafa75372e1bf..d4c7cd7bd9b63 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 diff --git a/tests/baselines/reference/contextualThisTypeInJavascript.symbols b/tests/baselines/reference/contextualThisTypeInJavascript.symbols new file mode 100644 index 0000000000000..6c146d654d0f5 --- /dev/null +++ b/tests/baselines/reference/contextualThisTypeInJavascript.symbols @@ -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)) + } +} + diff --git a/tests/baselines/reference/contextualThisTypeInJavascript.types b/tests/baselines/reference/contextualThisTypeInJavascript.types new file mode 100644 index 0000000000000..c3ec41230711d --- /dev/null +++ b/tests/baselines/reference/contextualThisTypeInJavascript.types @@ -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 + } +} + diff --git a/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts b/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts new file mode 100644 index 0000000000000..8ccbc65f545c3 --- /dev/null +++ b/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts @@ -0,0 +1,12 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true +// @Filename: context.js +const obj = { + prop: 2, + method() { + this; + this.prop; + this.method; + } +} From 95f5bc1ee0637af0ad3c51946629e5d127df1ac6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 18 Jul 2017 10:01:22 -0700 Subject: [PATCH 2/2] Add unknown property to test of contextual this type --- .../reference/contextualThisTypeInJavascript.symbols | 3 +++ .../reference/contextualThisTypeInJavascript.types | 7 ++++++- .../types/thisType/contextualThisTypeInJavascript.ts | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/baselines/reference/contextualThisTypeInJavascript.symbols b/tests/baselines/reference/contextualThisTypeInJavascript.symbols index 6c146d654d0f5..c1339014a8850 100644 --- a/tests/baselines/reference/contextualThisTypeInJavascript.symbols +++ b/tests/baselines/reference/contextualThisTypeInJavascript.symbols @@ -20,6 +20,9 @@ const obj = { >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)) } } diff --git a/tests/baselines/reference/contextualThisTypeInJavascript.types b/tests/baselines/reference/contextualThisTypeInJavascript.types index c3ec41230711d..355b7295e6e97 100644 --- a/tests/baselines/reference/contextualThisTypeInJavascript.types +++ b/tests/baselines/reference/contextualThisTypeInJavascript.types @@ -1,7 +1,7 @@ === 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, 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 @@ -22,6 +22,11 @@ const obj = { >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 } } diff --git a/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts b/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts index 8ccbc65f545c3..53e6c1bae7ebf 100644 --- a/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts +++ b/tests/cases/conformance/types/thisType/contextualThisTypeInJavascript.ts @@ -8,5 +8,6 @@ const obj = { this; this.prop; this.method; + this.unknown; // ok, obj has a string indexer } }