Skip to content

never return type not handled when function has implicit type #60609

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

Closed
jorrit opened this issue Nov 26, 2024 · 2 comments
Closed

never return type not handled when function has implicit type #60609

jorrit opened this issue Nov 26, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jorrit
Copy link

jorrit commented Nov 26, 2024

🔎 Search Terms

When a function has return type never it can be used as a kind of falsy check:

const test = (input: A|undefined): void => {
  if (input === undefined) {
    neverReturn();
  }

  // input is never undefined here.
  console.log(input.id);
}

This does not work in all cases, as demonstrated on the playground link. Only when the const has an explicit type never is handled correctly.

🕗 Version & Regression Information

It does not work in the any TypeScript version available at the PlayGround.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=3.8.3#code/JYOwLgpgTgZghgYwgAgILIN4ChnOAEwC5kQBXAWwCNoBuLAXyywQHsQBnMEiAN2gCUIYUlBABGZAF5kACgCUxEL2hSAfJhzIwACygsA7t0MBRKHqgyA5Kkty69OszactEThOkzQAB1JhiqAA+pCD4EDCgEPgKyDwsBGoauMAwsj5+UpLSIWERStFJuNx8UILComLydLiMmgD0dcgAkiC+XAhwIMicwAA2vcjUyDnhkfjIiKxQ+KAA5losyAAqAMoAdJqsHCy9EGu9LLNerX5rBHYMTFsuSiVlIiAATMTyibcqnjHvUInYuDp6QxKExmFgWay2eyOa5cSCcR5SNInfxoYKhUb5GJxBKSdR-PCpY5tTLZdF5KJyQq4b73USPKqaWq4BrNZF4djIMKQKDkMYLQYoEAsLgjcn4Da4a47PYHI7pMBnaL2LBAA

💻 Code

interface A {
  id: number;
}

const neverReturn1 = (): never => {
  throw new Error('A');
};

const test1 = (input: A|undefined): void => {
  if (input === undefined) {
    neverReturn1();
  }

  // Input can still be undefined according to TS.
  console.log(input.id);
}

const neverReturn2: () => never = (): never => {
  throw new Error('A');
};

const test2 = (input: A|undefined): void => {
  if (input === undefined) {
    neverReturn2();
  }

  // Input is determined to be not undefined.
  console.log(input.id);
}

🙁 Actual behavior

Error: Object is possibly 'undefined'. in test1.

🙂 Expected behavior

No errors, because neverReturn1() never returns.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 26, 2024

This is working as intended, see #32695. There are so many duplicates of this already. This limitation is for performance reasons.

A function call is analyzed as an assertion call or never-returning call when

  • the call occurs as a top-level expression statement, and
  • the call specifies a single identifier or a dotted sequence of identifiers for the function name, and
  • each identifier in the function name references an entity with an explicit type, and
  • the function name resolves to a function type with an asserts return type or an explicit never return type annotation.

Your search terms are rather excessive, no wonder you can't find a duplicate like this. I suggest to use a few related words as search terms, often including the modifier in:title. Definitely don't enter entire explanations and even code examples as the search terms.

@jakebailey jakebailey added the Duplicate An existing issue was already created label Nov 26, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2024
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

No branches or pull requests

4 participants