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 Map<string,boolean> reuturn type boolean | undefined #43860

Closed
HomyeeKing opened this issue Apr 28, 2021 · 4 comments
Closed

why Map<string,boolean> reuturn type boolean | undefined #43860

HomyeeKing opened this issue Apr 28, 2021 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@HomyeeKing
Copy link

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

  • This is a crash

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function test(id:string,map:Map<string,boolean>):boolean{
    if(map.has(id)){
        return map.get(id)  // why the type here is boolean | undefined? 
    }
}

πŸ™ Actual behavior

Type 'boolean | undefined' is not assignable to type 'boolean'.

πŸ™‚ Expected behavior

because I've set the type is Map<string,boolean>, so it is boolean, no way undefined

@nmain
Copy link

nmain commented Apr 28, 2021

Duplicate of #13086
Search terms: map has narrowing

@HomyeeKing
Copy link
Author

so in this case , we've done type guard by if condition, so it does exist and won't be deleted in common use case

@nmain
Copy link

nmain commented Apr 28, 2021

I agree, the code is sound, but tsc has no way to make this kind of reasoning.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Apr 28, 2021
@typescript-bot
Copy link
Collaborator

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.

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