-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Return type annotations ignored with recursive closures using JSDoc #31836
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
Comments
A helpful fellow on StackOverflow suggested using a /**
* @returns {function(): number}
*/
function circular() {
let rand = Math.random();
return /** @returns {number} */ function tryAgain() {
if (rand < 0.5) {
return rand;
}
rand = Math.random();
return tryAgain();
};
} |
The same problem occurs with generator functions when |
|
@boazblake You introduced a bug into the link you posted. You added a line break after the return statement: return /** @returns {number} */
function tryAgain() {
if (rand < 0.5) { You cannot have a return statement on its own line. Automatic Semicolon Insertion will insert a semicolon directly after the return; /** @returns {number} */
function tryAgain() {
if (rand < 0.5) { To fix your issue, delete the extraneous line break. The workaround then works as advertised: return /** @returns {number} */ function tryAgain() {
if (rand < 0.5) { |
@delventhalz Thank you 👍🏼 |
TypeScript Version: 3.5.1
Search Terms:
Code
I have a fairly complex use case in a project which uses vanilla JS with type annotations in JSDoc comments. The long and the short is that there is a function which returns a function, which may recursively call itself and will reassign some closure variables.
Here is a silly example which gets the point across and demonstrates the same issue:
Expected behavior:
TypeScript should know that the return type of
tryAgain
is a number.Actual behavior:
When run with:
tsc --allowJs --checkJs --noEmit --strict --target ES2017 *.js
The following error is thrown:
At the very least, this error seems pretty erroneous. The
tryAgain
function has two return type annotations (I have tried both styles in an attempt to fix this). The larger issue is that I need some way to get TypeScript to compile this code without a massive refactor.Playground Link:
None (code is JavaScript).
Related Issues:
This has a circular reference similar to #26623. However for that issue the solution was to add an explicit return type annotation. In my case (perhaps because I am using JSDoc), TypeScript seems to be ignoring all explicit annotations.
The text was updated successfully, but these errors were encountered: