Skip to content

Explicitly typed prototype assignments are context sensitive #25688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4713,6 +4713,10 @@ namespace ts {
// function/class/{} assignments are fresh declarations, not property assignments, so only add prototype assignments
const specialDeclaration = getAssignedJavascriptInitializer(symbol.valueDeclaration);
if (specialDeclaration) {
const tag = getJSDocTypeTag(specialDeclaration);
if (tag && tag.typeExpression) {
return getTypeFromTypeNode(tag.typeExpression);
}
return getWidenedLiteralType(checkExpressionCached(specialDeclaration));
}
const types: Type[] = [];
Expand Down Expand Up @@ -5080,7 +5084,7 @@ namespace ts {
}

function getJSInitializerType(decl: Node, symbol: Symbol, init: Expression | undefined): Type | undefined {
if (init && isInJavaScriptFile(init) && isObjectLiteralExpression(init)) {
if (init && isInJavaScriptFile(init) && isObjectLiteralExpression(init) && init.properties.length === 0) {
const exports = createSymbolTable();
while (isBinaryExpression(decl) || isPropertyAccessExpression(decl)) {
const s = getSymbolOfNode(decl);
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/chainedPrototypeAssignment.types
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ A.prototype = B.prototype = {
>A : typeof A
>prototype : { [x: string]: any; m(n: number): number; }
>B.prototype = { /** @param {number} n */ m(n) { return n + 1 }} : { [x: string]: any; m(n: number): number; }
>B.prototype : { [x: string]: any; }
>B.prototype : { [x: string]: any; m(n: number): number; }
>B : typeof B
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(n: number): number; }
>{ /** @param {number} n */ m(n) { return n + 1 }} : { [x: string]: any; m(n: number): number; }

/** @param {number} n */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
tests/cases/conformance/salsa/mod.js(5,7): error TS7006: Parameter 'n' implicitly has an 'any' type.
tests/cases/conformance/salsa/test.js(52,7): error TS7006: Parameter 'n' implicitly has an 'any' type.
tests/cases/conformance/salsa/test.js(70,7): error TS7006: Parameter 'n' implicitly has an 'any' type.


==== tests/cases/conformance/salsa/test.js (2 errors) ====
==== tests/cases/conformance/salsa/test.js (1 errors) ====
/** @typedef {{
status: 'done'
m(n: number): void
Expand Down Expand Up @@ -76,13 +75,11 @@ tests/cases/conformance/salsa/test.js(70,7): error TS7006: Parameter 'n' implici
F.prototype = {
status: "done",
m(n) { }
~
!!! error TS7006: Parameter 'n' implicitly has an 'any' type.
}

==== tests/cases/conformance/salsa/mod.js (1 errors) ====
// module.exports assignment
/** @type {{ status: 'done' }} */
/** @type {{ status: 'done', m(n: number): void }} */
module.exports = {
status: "done",
m(n) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ F.prototype = {

=== tests/cases/conformance/salsa/mod.js ===
// module.exports assignment
/** @type {{ status: 'done' }} */
/** @type {{ status: 'done', m(n: number): void }} */
module.exports = {
>module : Symbol(export=, Decl(mod.js, 0, 0))
>exports : Symbol(export=, Decl(mod.js, 0, 0))
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/contextualTypedSpecialAssignment.types
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,24 @@ function F() {
}
/** @type {DoneStatus} */
F.prototype = {
>F.prototype = { status: "done", m(n) { }} : { status: string; m(n: any): void; }
>F.prototype : { [x: string]: any; }
>F.prototype = { status: "done", m(n) { }} : { status: "done"; m(n: number): void; }
>F.prototype : { status: "done"; m(n: number): void; }
>F : typeof F
>prototype : { [x: string]: any; }
>{ status: "done", m(n) { }} : { status: string; m(n: any): void; }
>prototype : { status: "done"; m(n: number): void; }
>{ status: "done", m(n) { }} : { status: "done"; m(n: number): void; }

status: "done",
>status : string
>status : "done"
>"done" : "done"

m(n) { }
>m : (n: any) => void
>n : any
>m : (n: number) => void
>n : number
}

=== tests/cases/conformance/salsa/mod.js ===
// module.exports assignment
/** @type {{ status: 'done' }} */
/** @type {{ status: 'done', m(n: number): void }} */
module.exports = {
>module.exports = { status: "done", m(n) { }} : { status: string; m(n: any): void; }
>module.exports : any
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/typeFromPropertyAssignment11.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var Inner = function() {}

Inner.prototype = {
>Inner.prototype = { m() { }, i: 1} : { [x: string]: any; m(): void; i: number; }
>Inner.prototype : { [x: string]: any; }
>Inner.prototype : { [x: string]: any; m(): void; i: number; }
>Inner : typeof Inner
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(): void; i: number; }
>{ m() { }, i: 1} : { [x: string]: any; m(): void; i: number; }

m() { },
Expand All @@ -21,18 +21,18 @@ Inner.prototype = {
Inner.prototype.j = 2
>Inner.prototype.j = 2 : 2
>Inner.prototype.j : any
>Inner.prototype : { [x: string]: any; }
>Inner.prototype : { [x: string]: any; m(): void; i: number; }
>Inner : typeof Inner
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(): void; i: number; }
>j : any
>2 : 2

/** @type {string} */
Inner.prototype.k;
>Inner.prototype.k : any
>Inner.prototype : { [x: string]: any; }
>Inner.prototype : { [x: string]: any; m(): void; i: number; }
>Inner : typeof Inner
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(): void; i: number; }
>k : any

var inner = new Inner()
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/typeFromPropertyAssignment13.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Outer.Inner = function() {}

Outer.Inner.prototype = {
>Outer.Inner.prototype = { m() { }, i: 1} : { [x: string]: any; m(): void; i: number; }
>Outer.Inner.prototype : { [x: string]: any; }
>Outer.Inner.prototype : { [x: string]: any; m(): void; i: number; }
>Outer.Inner : typeof Inner
>Outer : typeof Outer
>Inner : typeof Inner
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(): void; i: number; }
>{ m() { }, i: 1} : { [x: string]: any; m(): void; i: number; }

m() { },
Expand All @@ -30,22 +30,22 @@ Outer.Inner.prototype = {
Outer.Inner.prototype.j = 2
>Outer.Inner.prototype.j = 2 : 2
>Outer.Inner.prototype.j : any
>Outer.Inner.prototype : { [x: string]: any; }
>Outer.Inner.prototype : { [x: string]: any; m(): void; i: number; }
>Outer.Inner : typeof Inner
>Outer : typeof Outer
>Inner : typeof Inner
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(): void; i: number; }
>j : any
>2 : 2

/** @type {string} */
Outer.Inner.prototype.k;
>Outer.Inner.prototype.k : any
>Outer.Inner.prototype : { [x: string]: any; }
>Outer.Inner.prototype : { [x: string]: any; m(): void; i: number; }
>Outer.Inner : typeof Inner
>Outer : typeof Outer
>Inner : typeof Inner
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; m(): void; i: number; }
>k : any

var inner = new Outer.Inner()
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/typeFromPropertyAssignment14.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ var Outer = {};

=== tests/cases/conformance/salsa/work.js ===
Outer.Inner = function () {}
>Outer.Inner = function () {} : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner = function () {} : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer : typeof Outer
>Inner : { (): void; prototype: { [x: string]: any; }; }
>function () {} : { (): void; prototype: { [x: string]: any; }; }
>Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>function () {} : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }

Outer.Inner.prototype = {
>Outer.Inner.prototype = { x: 1, m() { }} : { [x: string]: any; x: number; m(): void; }
>Outer.Inner.prototype : { [x: string]: any; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner.prototype : { [x: string]: any; x: number; m(): void; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer : typeof Outer
>Inner : { (): void; prototype: { [x: string]: any; }; }
>prototype : { [x: string]: any; }
>Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>prototype : { [x: string]: any; x: number; m(): void; }
>{ x: 1, m() { }} : { [x: string]: any; x: number; m(): void; }

x: 1,
Expand Down Expand Up @@ -47,9 +47,9 @@ inner.m()
var inno = new Outer.Inner()
>inno : { [x: string]: any; x: number; m(): void; }
>new Outer.Inner() : { [x: string]: any; x: number; m(): void; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer : typeof Outer
>Inner : { (): void; prototype: { [x: string]: any; }; }
>Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }

inno.x
>inno.x : number
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/typeFromPropertyAssignment16.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ var Outer = {};
>{} : { [x: string]: any; }

Outer.Inner = function () {}
>Outer.Inner = function () {} : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner = function () {} : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer : typeof Outer
>Inner : { (): void; prototype: { [x: string]: any; }; }
>function () {} : { (): void; prototype: { [x: string]: any; }; }
>Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>function () {} : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }

Outer.Inner.prototype = {
>Outer.Inner.prototype = { x: 1, m() { }} : { [x: string]: any; x: number; m(): void; }
>Outer.Inner.prototype : { [x: string]: any; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner.prototype : { [x: string]: any; x: number; m(): void; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer : typeof Outer
>Inner : { (): void; prototype: { [x: string]: any; }; }
>prototype : { [x: string]: any; }
>Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>prototype : { [x: string]: any; x: number; m(): void; }
>{ x: 1, m() { }} : { [x: string]: any; x: number; m(): void; }

x: 1,
Expand Down Expand Up @@ -45,9 +45,9 @@ inner.m()
var inno = new Outer.Inner()
>inno : { [x: string]: any; x: number; m(): void; }
>new Outer.Inner() : { [x: string]: any; x: number; m(): void; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; }; }
>Outer.Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }
>Outer : typeof Outer
>Inner : { (): void; prototype: { [x: string]: any; }; }
>Inner : { (): void; prototype: { [x: string]: any; x: number; m(): void; }; }

inno.x
>inno.x : number
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/typeFromPropertyAssignment27.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ function C() { this.p = 1; }

C.prototype = { q: 2 };
>C.prototype = { q: 2 } : { [x: string]: any; q: number; }
>C.prototype : { [x: string]: any; }
>C.prototype : { [x: string]: any; q: number; }
>C : typeof C
>prototype : { [x: string]: any; }
>prototype : { [x: string]: any; q: number; }
>{ q: 2 } : { [x: string]: any; q: number; }
>q : number
>2 : 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ F.prototype = {

// @Filename: mod.js
// module.exports assignment
/** @type {{ status: 'done' }} */
/** @type {{ status: 'done', m(n: number): void }} */
module.exports = {
status: "done",
m(n) { }
Expand Down