diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e4f2b0e6da9..16e982bfff3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -28947,7 +28947,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return undefined; } } - return isInJSFile(decl) ? undefined : getTypeOfExpression(binaryExpression.left); + return isInJSFile(decl) || decl === binaryExpression.left ? undefined : getTypeOfExpression(binaryExpression.left); } case AssignmentDeclarationKind.ExportsProperty: case AssignmentDeclarationKind.Prototype: diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE2.js b/tests/baselines/reference/contextualReturnTypeOfIIFE2.js new file mode 100644 index 00000000000..c7ea6366750 --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE2.js @@ -0,0 +1,19 @@ +//// [contextualReturnTypeOfIIFE2.ts] +declare namespace app { + function foo(): void; +} + +app.foo.bar = (function () { + const someFun = (arg: number) => {}; + return { someFun }; +})(); + +app.foo.bar.someFun(1); + + +//// [contextualReturnTypeOfIIFE2.js] +app.foo.bar = (function () { + var someFun = function (arg) { }; + return { someFun: someFun }; +})(); +app.foo.bar.someFun(1); diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE2.symbols b/tests/baselines/reference/contextualReturnTypeOfIIFE2.symbols new file mode 100644 index 00000000000..6e7470d4541 --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE2.symbols @@ -0,0 +1,33 @@ +=== tests/cases/compiler/contextualReturnTypeOfIIFE2.ts === +declare namespace app { +>app : Symbol(app, Decl(contextualReturnTypeOfIIFE2.ts, 0, 0), Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) + + function foo(): void; +>foo : Symbol(foo, Decl(contextualReturnTypeOfIIFE2.ts, 0, 23), Decl(contextualReturnTypeOfIIFE2.ts, 4, 4)) +} + +app.foo.bar = (function () { +>app.foo.bar : Symbol(app.foo.bar, Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) +>app.foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE2.ts, 0, 23), Decl(contextualReturnTypeOfIIFE2.ts, 4, 4)) +>app : Symbol(app, Decl(contextualReturnTypeOfIIFE2.ts, 0, 0), Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) +>foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE2.ts, 0, 23), Decl(contextualReturnTypeOfIIFE2.ts, 4, 4)) +>bar : Symbol(app.foo.bar, Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) + + const someFun = (arg: number) => {}; +>someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE2.ts, 5, 7)) +>arg : Symbol(arg, Decl(contextualReturnTypeOfIIFE2.ts, 5, 19)) + + return { someFun }; +>someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE2.ts, 6, 10)) + +})(); + +app.foo.bar.someFun(1); +>app.foo.bar.someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE2.ts, 6, 10)) +>app.foo.bar : Symbol(app.foo.bar, Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) +>app.foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE2.ts, 0, 23), Decl(contextualReturnTypeOfIIFE2.ts, 4, 4)) +>app : Symbol(app, Decl(contextualReturnTypeOfIIFE2.ts, 0, 0), Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) +>foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE2.ts, 0, 23), Decl(contextualReturnTypeOfIIFE2.ts, 4, 4)) +>bar : Symbol(app.foo.bar, Decl(contextualReturnTypeOfIIFE2.ts, 2, 1)) +>someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE2.ts, 6, 10)) + diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE2.types b/tests/baselines/reference/contextualReturnTypeOfIIFE2.types new file mode 100644 index 00000000000..2ad9004733e --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE2.types @@ -0,0 +1,41 @@ +=== tests/cases/compiler/contextualReturnTypeOfIIFE2.ts === +declare namespace app { +>app : typeof app + + function foo(): void; +>foo : typeof foo +} + +app.foo.bar = (function () { +>app.foo.bar = (function () { const someFun = (arg: number) => {}; return { someFun };})() : { someFun: (arg: number) => void; } +>app.foo.bar : { someFun: (arg: number) => void; } +>app.foo : typeof app.foo +>app : typeof app +>foo : typeof app.foo +>bar : { someFun: (arg: number) => void; } +>(function () { const someFun = (arg: number) => {}; return { someFun };})() : { someFun: (arg: number) => void; } +>(function () { const someFun = (arg: number) => {}; return { someFun };}) : () => { someFun: (arg: number) => void; } +>function () { const someFun = (arg: number) => {}; return { someFun };} : () => { someFun: (arg: number) => void; } + + const someFun = (arg: number) => {}; +>someFun : (arg: number) => void +>(arg: number) => {} : (arg: number) => void +>arg : number + + return { someFun }; +>{ someFun } : { someFun: (arg: number) => void; } +>someFun : (arg: number) => void + +})(); + +app.foo.bar.someFun(1); +>app.foo.bar.someFun(1) : void +>app.foo.bar.someFun : (arg: number) => void +>app.foo.bar : { someFun: (arg: number) => void; } +>app.foo : typeof app.foo +>app : typeof app +>foo : typeof app.foo +>bar : { someFun: (arg: number) => void; } +>someFun : (arg: number) => void +>1 : 1 + diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE3.js b/tests/baselines/reference/contextualReturnTypeOfIIFE3.js new file mode 100644 index 00000000000..fc6096bc225 --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE3.js @@ -0,0 +1,21 @@ +//// [contextualReturnTypeOfIIFE3.ts] +declare namespace app { + var foo: { + bar: { + someFun: (arg: number) => void; + }; + }; +} + +app.foo.bar = (function () { + return { someFun(arg) {} }; +})(); + +app.foo.bar.someFun(1); + + +//// [contextualReturnTypeOfIIFE3.js] +app.foo.bar = (function () { + return { someFun: function (arg) { } }; +})(); +app.foo.bar.someFun(1); diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE3.symbols b/tests/baselines/reference/contextualReturnTypeOfIIFE3.symbols new file mode 100644 index 00000000000..d44c44f894c --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE3.symbols @@ -0,0 +1,40 @@ +=== tests/cases/compiler/contextualReturnTypeOfIIFE3.ts === +declare namespace app { +>app : Symbol(app, Decl(contextualReturnTypeOfIIFE3.ts, 0, 0)) + + var foo: { +>foo : Symbol(foo, Decl(contextualReturnTypeOfIIFE3.ts, 1, 5)) + + bar: { +>bar : Symbol(bar, Decl(contextualReturnTypeOfIIFE3.ts, 1, 12)) + + someFun: (arg: number) => void; +>someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE3.ts, 2, 10)) +>arg : Symbol(arg, Decl(contextualReturnTypeOfIIFE3.ts, 3, 16)) + + }; + }; +} + +app.foo.bar = (function () { +>app.foo.bar : Symbol(bar, Decl(contextualReturnTypeOfIIFE3.ts, 1, 12)) +>app.foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE3.ts, 1, 5)) +>app : Symbol(app, Decl(contextualReturnTypeOfIIFE3.ts, 0, 0)) +>foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE3.ts, 1, 5)) +>bar : Symbol(bar, Decl(contextualReturnTypeOfIIFE3.ts, 1, 12)) + + return { someFun(arg) {} }; +>someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE3.ts, 9, 10)) +>arg : Symbol(arg, Decl(contextualReturnTypeOfIIFE3.ts, 9, 19)) + +})(); + +app.foo.bar.someFun(1); +>app.foo.bar.someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE3.ts, 2, 10)) +>app.foo.bar : Symbol(bar, Decl(contextualReturnTypeOfIIFE3.ts, 1, 12)) +>app.foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE3.ts, 1, 5)) +>app : Symbol(app, Decl(contextualReturnTypeOfIIFE3.ts, 0, 0)) +>foo : Symbol(app.foo, Decl(contextualReturnTypeOfIIFE3.ts, 1, 5)) +>bar : Symbol(bar, Decl(contextualReturnTypeOfIIFE3.ts, 1, 12)) +>someFun : Symbol(someFun, Decl(contextualReturnTypeOfIIFE3.ts, 2, 10)) + diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE3.types b/tests/baselines/reference/contextualReturnTypeOfIIFE3.types new file mode 100644 index 00000000000..ba0dd8b2b59 --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE3.types @@ -0,0 +1,47 @@ +=== tests/cases/compiler/contextualReturnTypeOfIIFE3.ts === +declare namespace app { +>app : typeof app + + var foo: { +>foo : { bar: { someFun: (arg: number) => void; }; } + + bar: { +>bar : { someFun: (arg: number) => void; } + + someFun: (arg: number) => void; +>someFun : (arg: number) => void +>arg : number + + }; + }; +} + +app.foo.bar = (function () { +>app.foo.bar = (function () { return { someFun(arg) {} };})() : { someFun(arg: number): void; } +>app.foo.bar : { someFun: (arg: number) => void; } +>app.foo : { bar: { someFun: (arg: number) => void; }; } +>app : typeof app +>foo : { bar: { someFun: (arg: number) => void; }; } +>bar : { someFun: (arg: number) => void; } +>(function () { return { someFun(arg) {} };})() : { someFun(arg: number): void; } +>(function () { return { someFun(arg) {} };}) : () => { someFun(arg: number): void; } +>function () { return { someFun(arg) {} };} : () => { someFun(arg: number): void; } + + return { someFun(arg) {} }; +>{ someFun(arg) {} } : { someFun(arg: number): void; } +>someFun : (arg: number) => void +>arg : number + +})(); + +app.foo.bar.someFun(1); +>app.foo.bar.someFun(1) : void +>app.foo.bar.someFun : (arg: number) => void +>app.foo.bar : { someFun: (arg: number) => void; } +>app.foo : { bar: { someFun: (arg: number) => void; }; } +>app : typeof app +>foo : { bar: { someFun: (arg: number) => void; }; } +>bar : { someFun: (arg: number) => void; } +>someFun : (arg: number) => void +>1 : 1 + diff --git a/tests/cases/compiler/contextualReturnTypeOfIIFE2.ts b/tests/cases/compiler/contextualReturnTypeOfIIFE2.ts new file mode 100644 index 00000000000..18416655bb9 --- /dev/null +++ b/tests/cases/compiler/contextualReturnTypeOfIIFE2.ts @@ -0,0 +1,13 @@ +// @lib: esnext +// @noImplicitAny: true + +declare namespace app { + function foo(): void; +} + +app.foo.bar = (function () { + const someFun = (arg: number) => {}; + return { someFun }; +})(); + +app.foo.bar.someFun(1); diff --git a/tests/cases/compiler/contextualReturnTypeOfIIFE3.ts b/tests/cases/compiler/contextualReturnTypeOfIIFE3.ts new file mode 100644 index 00000000000..6f2ee4e1dd7 --- /dev/null +++ b/tests/cases/compiler/contextualReturnTypeOfIIFE3.ts @@ -0,0 +1,16 @@ +// @lib: esnext +// @noImplicitAny: true + +declare namespace app { + var foo: { + bar: { + someFun: (arg: number) => void; + }; + }; +} + +app.foo.bar = (function () { + return { someFun(arg) {} }; +})(); + +app.foo.bar.someFun(1);