Skip to content

Improve @overload's interactions with constructors #52577

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
merged 1 commit into from
Feb 2, 2023
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
9 changes: 8 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14456,7 +14456,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
for (const tag of node.tags) {
if (isJSDocOverloadTag(tag)) {
const jsDocSignature = tag.typeExpression;
if (jsDocSignature.type === undefined) {
if (jsDocSignature.type === undefined && !isConstructorDeclaration(decl)) {
reportImplicitAny(jsDocSignature, anyType);
}
result.push(getSignatureFromDeclaration(jsDocSignature));
Expand Down Expand Up @@ -14582,6 +14582,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (declaration.kind === SyntaxKind.Constructor) {
return getDeclaredTypeOfClassOrInterface(getMergedSymbol((declaration.parent as ClassDeclaration).symbol));
}
if (isJSDocSignature(declaration)) {
const root = getJSDocRoot(declaration);
if (root && isConstructorDeclaration(root.parent)) {
return getDeclaredTypeOfClassOrInterface(getMergedSymbol((root.parent.parent as ClassDeclaration).symbol));
}
}
if (isJSDocConstructSignature(declaration)) {
return getTypeFromTypeNode((declaration.parameters[0] as ParameterDeclaration).type!); // TODO: GH#18217
}
Expand Down Expand Up @@ -33771,6 +33777,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
declaration.kind !== SyntaxKind.Constructor &&
declaration.kind !== SyntaxKind.ConstructSignature &&
declaration.kind !== SyntaxKind.ConstructorType &&
!(isJSDocSignature(declaration) && getJSDocRoot(declaration)?.parent?.kind === SyntaxKind.Constructor) &&
!isJSDocConstructSignature(declaration) &&
Comment on lines +33780 to 33781
Copy link
Member

Choose a reason for hiding this comment

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

Is isJSDocConstructSignature supposed to do the check?

Copy link
Member Author

Choose a reason for hiding this comment

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

Confusingly, JSDocConstructSignature refers to closure type syntax: function(new: SomeClass) is equivalent to TS new (): SomeClass.

The naming scheme could be better though.

!isJSConstructor(declaration)) {

Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/overloadTag2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
tests/cases/conformance/jsdoc/overloadTag2.js(25,20): error TS7006: Parameter 'b' implicitly has an 'any' type.
tests/cases/conformance/jsdoc/overloadTag2.js(30,9): error TS2554: Expected 1-2 arguments, but got 0.


==== tests/cases/conformance/jsdoc/overloadTag2.js (2 errors) ====
export class Foo {
#a = true ? 1 : "1"
#b

/**
* Should not have an implicit any error, because constructor's return type is always implicit
* @constructor
* @overload
* @param {string} a
* @param {number} b
*/
/**
* @constructor
* @overload
* @param {number} a
*/
/**
* @constructor
* @overload
* @param {string} a
*//**
* @constructor
* @param {number | string} a
*/
constructor(a, b) {
~
!!! error TS7006: Parameter 'b' implicitly has an 'any' type.
this.#a = a
this.#b = b
}
}
var a = new Foo()
~~~~~~~~~
!!! error TS2554: Expected 1-2 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/jsdoc/overloadTag2.js:15:8: An argument for 'a' was not provided.
var b = new Foo('str')
var c = new Foo(2)
var d = new Foo('str', 2)

78 changes: 78 additions & 0 deletions tests/baselines/reference/overloadTag2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//// [overloadTag2.js]
export class Foo {
#a = true ? 1 : "1"
#b

/**
* Should not have an implicit any error, because constructor's return type is always implicit
* @constructor
* @overload
* @param {string} a
* @param {number} b
*/
/**
* @constructor
* @overload
* @param {number} a
*/
/**
* @constructor
* @overload
* @param {string} a
*//**
* @constructor
* @param {number | string} a
*/
constructor(a, b) {
this.#a = a
this.#b = b
}
}
var a = new Foo()
var b = new Foo('str')
var c = new Foo(2)
var d = new Foo('str', 2)


//// [overloadTag2.js]
export class Foo {
#a = true ? 1 : "1";
#b;
/**
* Should not have an implicit any error, because constructor's return type is always implicit
* @constructor
* @overload
* @param {string} a
* @param {number} b
*/
/**
* @constructor
* @overload
* @param {number} a
*/
/**
* @constructor
* @overload
* @param {string} a
*/ /**
* @constructor
* @param {number | string} a
*/
constructor(a, b) {
this.#a = a;
this.#b = b;
}
}
var a = new Foo();
var b = new Foo('str');
var c = new Foo(2);
var d = new Foo('str', 2);


//// [overloadTag2.d.ts]
export class Foo {
constructor(a: string, b: number);
constructor(a: number);
constructor(a: string);
#private;
}
61 changes: 61 additions & 0 deletions tests/baselines/reference/overloadTag2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
=== tests/cases/conformance/jsdoc/overloadTag2.js ===
export class Foo {
>Foo : Symbol(Foo, Decl(overloadTag2.js, 0, 0))

#a = true ? 1 : "1"
>#a : Symbol(Foo.#a, Decl(overloadTag2.js, 0, 18))

#b
>#b : Symbol(Foo.#b, Decl(overloadTag2.js, 1, 23))

/**
* Should not have an implicit any error, because constructor's return type is always implicit
* @constructor
* @overload
* @param {string} a
* @param {number} b
*/
/**
* @constructor
* @overload
* @param {number} a
*/
/**
* @constructor
* @overload
* @param {string} a
*//**
* @constructor
* @param {number | string} a
*/
constructor(a, b) {
>a : Symbol(a, Decl(overloadTag2.js, 24, 16))
>b : Symbol(b, Decl(overloadTag2.js, 24, 18))

this.#a = a
>this.#a : Symbol(Foo.#a, Decl(overloadTag2.js, 0, 18))
>this : Symbol(Foo, Decl(overloadTag2.js, 0, 0))
>a : Symbol(a, Decl(overloadTag2.js, 24, 16))

this.#b = b
>this.#b : Symbol(Foo.#b, Decl(overloadTag2.js, 1, 23))
>this : Symbol(Foo, Decl(overloadTag2.js, 0, 0))
>b : Symbol(b, Decl(overloadTag2.js, 24, 18))
}
}
var a = new Foo()
>a : Symbol(a, Decl(overloadTag2.js, 29, 3))
>Foo : Symbol(Foo, Decl(overloadTag2.js, 0, 0))

var b = new Foo('str')
>b : Symbol(b, Decl(overloadTag2.js, 30, 3))
>Foo : Symbol(Foo, Decl(overloadTag2.js, 0, 0))

var c = new Foo(2)
>c : Symbol(c, Decl(overloadTag2.js, 31, 3))
>Foo : Symbol(Foo, Decl(overloadTag2.js, 0, 0))

var d = new Foo('str', 2)
>d : Symbol(d, Decl(overloadTag2.js, 32, 3))
>Foo : Symbol(Foo, Decl(overloadTag2.js, 0, 0))

75 changes: 75 additions & 0 deletions tests/baselines/reference/overloadTag2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
=== tests/cases/conformance/jsdoc/overloadTag2.js ===
export class Foo {
>Foo : Foo

#a = true ? 1 : "1"
>#a : string | number
>true ? 1 : "1" : 1 | "1"
>true : true
>1 : 1
>"1" : "1"

#b
>#b : any

/**
* Should not have an implicit any error, because constructor's return type is always implicit
* @constructor
* @overload
* @param {string} a
* @param {number} b
*/
/**
* @constructor
* @overload
* @param {number} a
*/
/**
* @constructor
* @overload
* @param {string} a
*//**
* @constructor
* @param {number | string} a
*/
constructor(a, b) {
>a : string | number
>b : any

this.#a = a
>this.#a = a : string | number
>this.#a : string | number
>this : this
>a : string | number

this.#b = b
>this.#b = b : any
>this.#b : any
>this : this
>b : any
}
}
var a = new Foo()
>a : Foo
>new Foo() : Foo
>Foo : typeof Foo

var b = new Foo('str')
>b : Foo
>new Foo('str') : Foo
>Foo : typeof Foo
>'str' : "str"

var c = new Foo(2)
>c : Foo
>new Foo(2) : Foo
>Foo : typeof Foo
>2 : 2

var d = new Foo('str', 2)
>d : Foo
>new Foo('str', 2) : Foo
>Foo : typeof Foo
>'str' : "str"
>2 : 2

40 changes: 40 additions & 0 deletions tests/cases/conformance/jsdoc/overloadTag2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// @checkJs: true
// @allowJs: true
// @target: esnext
// @outdir: foo
// @declaration: true
// @filename: overloadTag2.js
// @strict: true
export class Foo {
#a = true ? 1 : "1"
#b

/**
* Should not have an implicit any error, because constructor's return type is always implicit
* @constructor
* @overload
* @param {string} a
* @param {number} b
*/
/**
* @constructor
* @overload
* @param {number} a
*/
/**
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

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

Does this test need to be written in such a way as to satisfy #52474?

Copy link
Member Author

@sandersn sandersn Feb 2, 2023

Choose a reason for hiding this comment

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

You mean putting all the tags into a single comment? No, this test showed that that requirement isn't feasible; the parser can't distinguish between the end of a return-less @overload and the beginning of the implementation's @params. So I reverted it and once again @overload is unlike all other jsdoc comments in checking all preceding /** comments.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, so #52474 is no longer a thing, okay.

* @constructor
* @overload
* @param {string} a
*//**
* @constructor
* @param {number | string} a
*/
constructor(a, b) {
this.#a = a
this.#b = b
}
}
var a = new Foo()
var b = new Foo('str')
var c = new Foo(2)
var d = new Foo('str', 2)