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

Possible type inference failure #44218

Closed
FUDCo opened this issue May 22, 2021 · 2 comments · Fixed by #56908
Closed

Possible type inference failure #44218

FUDCo opened this issue May 22, 2021 · 2 comments · Fixed by #56908
Labels
Duplicate An existing issue was already created

Comments

@FUDCo
Copy link

FUDCo commented May 22, 2021

Bug Report

🔎 Search Terms

typescript type inference failures

🕗 Version & Regression Information

First encountered in tsc 3.9.7, just verified in the playground with v4.2.3

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything (I read the whole FAQ and didn't find anything that seemed to match the kind of thing I'm talking about here)

⏯ Playground Link

Here

💻 Code

const fooMap = new Map<string, string>();

function example(key: string, value: string): () => string {
  let s = fooMap.get(key);
  if (s === undefined) {
    s = value;
  }
  const t = s;
  return () => s; // replace s with t here to make the error go away
}

🙁 Actual behavior

On the final line of the sample (the return statement), TS complains:

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

even though at the location in question s is certain to be of type string.
If you replace s with t, defined on the line immediately above the return statement, the error goes away.

🙂 Expected behavior

tsc should not whine about a type error on that return statement.

@MartinJohns
Copy link
Contributor

Duplicate of #9998.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label May 25, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants