Skip to content

Check overload tag against implementation #52474

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
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
17 changes: 16 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ import {
JSDocMemberName,
JSDocNullableType,
JSDocOptionalType,
JSDocOverloadTag,
JSDocParameterTag,
JSDocPrivateTag,
JSDocPropertyLikeTag,
Expand Down Expand Up @@ -38616,6 +38617,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
lastSeenNonAmbientDeclaration = node as FunctionLikeDeclaration;
}
}
if (isInJSFile(current) && isFunctionLike(current) && current.jsDoc) {
for (const node of current.jsDoc) {
if (node.tags) {
for (const tag of node.tags) {
if (isJSDocOverloadTag(tag)) {
hasOverloads = true;
}
}
}
}
}
}
}

Expand Down Expand Up @@ -38667,8 +38679,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const bodySignature = getSignatureFromDeclaration(bodyDeclaration);
for (const signature of signatures) {
if (!isImplementationCompatibleWithOverload(bodySignature, signature)) {
const errorNode = signature.declaration && isJSDocSignature(signature.declaration)
? (signature.declaration.parent as JSDocOverloadTag | JSDocCallbackTag).tagName
: signature.declaration;
addRelatedInfo(
error(signature.declaration, Diagnostics.This_overload_signature_is_not_compatible_with_its_implementation_signature),
error(errorNode, Diagnostics.This_overload_signature_is_not_compatible_with_its_implementation_signature),
createDiagnosticForNode(bodyDeclaration, Diagnostics.The_implementation_signature_is_declared_here)
);
break;
Expand Down
73 changes: 73 additions & 0 deletions tests/baselines/reference/overloadTag1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
tests/cases/conformance/jsdoc/overloadTag1.js(7,5): error TS2394: This overload signature is not compatible with its implementation signature.
tests/cases/conformance/jsdoc/overloadTag1.js(25,10): error TS2769: No overload matches this call.
Overload 1 of 2, '(a: number, b: number): number', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'number'.
Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'boolean'.
tests/cases/conformance/jsdoc/overloadTag1.js(43,1): error TS2769: No overload matches this call.
Overload 1 of 2, '(a: number, b: number): number', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'number'.
Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'boolean'.


==== tests/cases/conformance/jsdoc/overloadTag1.js (3 errors) ====
/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
~~~~~~~~
!!! error TS2394: This overload signature is not compatible with its implementation signature.
!!! related TS2750 tests/cases/conformance/jsdoc/overloadTag1.js:16:17: The implementation signature is declared here.
* @param {string} a
* @param {boolean} b
* @returns {string}
*
* @param {string | number} a
* @param {string | number} b
* @returns {string | number}
*/
export function overloaded(a,b) {
if (typeof a === "string" && typeof b === "string") {
return a + b;
} else if (typeof a === "number" && typeof b === "number") {
return a + b;
}
throw new Error("Invalid arguments");
}
var o1 = overloaded(1,2)
var o2 = overloaded("zero", "one")
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(a: number, b: number): number', gave the following error.
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'number'.
!!! error TS2769: Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
var o3 = overloaded("a",false)

/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*/
export function uncheckedInternally(a, b) {
return a + b;
}
uncheckedInternally(1,2)
uncheckedInternally("zero", "one")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(a: number, b: number): number', gave the following error.
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'number'.
!!! error TS2769: Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.

102 changes: 102 additions & 0 deletions tests/baselines/reference/overloadTag1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//// [overloadTag1.js]
/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*
* @param {string | number} a
* @param {string | number} b
* @returns {string | number}
*/
export function overloaded(a,b) {
if (typeof a === "string" && typeof b === "string") {
return a + b;
} else if (typeof a === "number" && typeof b === "number") {
return a + b;
}
throw new Error("Invalid arguments");
}
var o1 = overloaded(1,2)
var o2 = overloaded("zero", "one")
var o3 = overloaded("a",false)

/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*/
export function uncheckedInternally(a, b) {
return a + b;
}
uncheckedInternally(1,2)
uncheckedInternally("zero", "one")


//// [overloadTag1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.uncheckedInternally = exports.overloaded = void 0;
/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*
* @param {string | number} a
* @param {string | number} b
* @returns {string | number}
*/
function overloaded(a, b) {
if (typeof a === "string" && typeof b === "string") {
return a + b;
}
else if (typeof a === "number" && typeof b === "number") {
return a + b;
}
throw new Error("Invalid arguments");
}
exports.overloaded = overloaded;
var o1 = overloaded(1, 2);
var o2 = overloaded("zero", "one");
var o3 = overloaded("a", false);
/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*/
function uncheckedInternally(a, b) {
return a + b;
}
exports.uncheckedInternally = uncheckedInternally;
uncheckedInternally(1, 2);
uncheckedInternally("zero", "one");


//// [overloadTag1.d.ts]
export function overloaded(a: number, b: number): number;
export function overloaded(a: string, b: boolean): string;
export function uncheckedInternally(a: number, b: number): number;
export function uncheckedInternally(a: string, b: boolean): string;
78 changes: 78 additions & 0 deletions tests/baselines/reference/overloadTag1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
=== tests/cases/conformance/jsdoc/overloadTag1.js ===
/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*
* @param {string | number} a
* @param {string | number} b
* @returns {string | number}
*/
export function overloaded(a,b) {
>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 0, 0))
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))

if (typeof a === "string" && typeof b === "string") {
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))

return a + b;
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))

} else if (typeof a === "number" && typeof b === "number") {
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))

return a + b;
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))
}
throw new Error("Invalid arguments");
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}
var o1 = overloaded(1,2)
>o1 : Symbol(o1, Decl(overloadTag1.js, 23, 3))
>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 0, 0))

var o2 = overloaded("zero", "one")
>o2 : Symbol(o2, Decl(overloadTag1.js, 24, 3))
>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 0, 0))

var o3 = overloaded("a",false)
>o3 : Symbol(o3, Decl(overloadTag1.js, 25, 3))
>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 0, 0))

/**
* @overload
* @param {number} a
* @param {number} b
* @returns {number}
*
* @overload
* @param {string} a
* @param {boolean} b
* @returns {string}
*/
export function uncheckedInternally(a, b) {
>uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 25, 30))
>a : Symbol(a, Decl(overloadTag1.js, 38, 36))
>b : Symbol(b, Decl(overloadTag1.js, 38, 38))

return a + b;
>a : Symbol(a, Decl(overloadTag1.js, 38, 36))
>b : Symbol(b, Decl(overloadTag1.js, 38, 38))
}
uncheckedInternally(1,2)
>uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 25, 30))

uncheckedInternally("zero", "one")
>uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 25, 30))

Loading