Skip to content
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

Why the recursive function's return type inferenced as any? #53978

Closed
nnnnoel opened this issue Apr 24, 2023 · 8 comments
Closed

Why the recursive function's return type inferenced as any? #53978

nnnnoel opened this issue Apr 24, 2023 · 8 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@nnnnoel
Copy link

nnnnoel commented Apr 24, 2023

Bug Report

when write recursive function via generator function or function,
return type inferenced as any.
the general purpose for recursive function, return type can be inferenced as correct return type.

🔎 Search Terms

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

image

function mock__fstat(name: string) {
    if (Math.random() > 0.5) {
        return {
            isDirectory: true,
            isFile: false,
        };
    }
    return {
        isDirectory: false,
        isFile: true,
    };
}
function* walk(dir: string) {
    if (mock__fstat(dir).isDirectory) {
        yield* walk(dir.split('/').slice(1).join('/'));
    }
    yield dir;
}
const a = walk('/a/b/c'); // will be AsyncGenerator<string>, but inferenced type is any

🙁 Actual behavior

walk function's return type is any

🙂 Expected behavior

walk function's return type is AsyncGenerator

@MartinJohns
Copy link
Contributor

Your playground link does not work. Duplicate of #32523.

@nnnnoel
Copy link
Author

nnnnoel commented Apr 24, 2023

@MartinJohns link updated!

@nnnnoel
Copy link
Author

nnnnoel commented Apr 24, 2023

I thought #32523 is not focused on recursive function's type.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Apr 24, 2023
@RyanCavanaugh
Copy link
Member

The error message tells you why?

because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.

@nnnnoel
Copy link
Author

nnnnoel commented Apr 25, 2023

@RyanCavanaugh but i cannot understand why this expression didn't inference type implicitly.
in this case, walk function returns AsyncGenerator<string> absolutely but recursive function call's return type inferenced as any.
why didn't skip the recursive function call in return type inference flow?

@fatcerberus
Copy link

why didn't skip the recursive function call in return type inference flow?

#53995 is probably relevant

@nnnnoel
Copy link
Author

nnnnoel commented Apr 25, 2023

@fatcerberus omg thanks a lot!

@nnnnoel nnnnoel changed the title Why the recursive function's return type is inferenced as any? Why the recursive function's return type inferenced as any? Apr 25, 2023
@microsoft-github-policy-service

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants