Skip to content

Arrow function don't see never as a return type, while normal functions do #55351

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
Asaf-S opened this issue Aug 13, 2023 Β· 6 comments
Closed

Arrow function don't see never as a return type, while normal functions do #55351

Asaf-S opened this issue Aug 13, 2023 Β· 6 comments
Labels
Duplicate An existing issue was already created

Comments

@Asaf-S
Copy link

Asaf-S commented Aug 13, 2023

πŸ”Ž Search Terms

"typescript function that throws", "typescript function that always throws"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.7 + 5.1

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.6.4#code/GYVwdgxgLglg9mABBBBnOAbApgUQE55x4CCYAJgCoAWhA7gPoCMAFFgUQLJaqoCGA5lgBciVFDwww-AJQiwWAG5tEAb0QB6dYgA8AWn2IAckQC2vDIlCRYCALAAoRE+RpMWAHRtCeVuzxceASxpAG4HZ0QoGjhaRHlY-G9fbwC+QVCHAF8HBxQwMURaPF4ABxK2ADFwaHgwJkQAXkRmdBMsADVePDkQEwAjNmlGgD5VcMQYYGbWjq6dRABWIZVxiLwsKBA8JBnOvDDHREzELAxULDHDiLz0bESiUkpohhYAA3u8Qt5USzhwMgAhK8ModsvZMgdcmgoC58m4Po9qHR6AAmRrNLycbhpYSicSSGRyRTKBqjNQaLR6AzEdixKw1OyHG5uTx+ZJYwLpA5OKJ0OJYBJszH+bFBEFgqH5GFFUrlPBVay1VHolpwNp7Hr9QYjVQUoxwGHmCwoMgXEq8KI-dabbaIXiIBTmEAeZoAdgADABmd3SVYTKaq9VzbSLZZ+tYbLY7NWzfZ+46nc6XCLXVx3PyI56o5jvPxfH7AP7kIEgiJgiEOIA

πŸ’» Code

function consoleErrorAndThrow_1(errorMessage: string): never { // <--- Normal function
    console.error(errorMessage);
    throw new Error(errorMessage);
}

const wrapperFunction_1 = (someVar: number) => {
  if (someVar < 5) {
      return someVar;
  } else {
      consoleErrorAndThrow_1(`Error was found!`);
  }
};

const consoleErrorAndThrow_2 = (errorMessage: string): never => {  // <--- Arrow function
  console.error(errorMessage);
  throw new Error(errorMessage);
}

const wrapperFunction_2 = (someVar: number) => { // Not all code paths return a value. (7030)
    if (someVar < 5) {
        return someVar;
    } else {
        consoleErrorAndThrow_2(`Error was found!`);
    }
};

πŸ™ Actual behavior

Get error Not all code paths return a value. (7030)

πŸ™‚ Expected behavior

No error

@Asaf-S
Copy link
Author

Asaf-S commented Aug 13, 2023

Is it a bug or an undocumented feature?

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 13, 2023 β€’

It's working as intended and documented here: #32695

This issue has been raised many many times. It should be added to the FAQ.

@fatcerberus
Copy link

documented here: #32695

In particular, this rule:

each identifier in the function name references an entity with an explicit type

consoleErrorAndThrow_2 has no type annotation.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 14, 2023
@Asaf-S
Copy link
Author

Asaf-S commented Aug 15, 2023

Setting the type of consoleErrorAndThrow_2 solves this issue, although I'm not really sure why, as the constant's type should be taken from its value.

const consoleErrorAndThrow_2: (errorMessage: string) => never = (errorMessage: string): never => {  // <--- Arrow function
  console.error(errorMessage);
  throw new Error(errorMessage);
}

@Asaf-S Asaf-S closed this as completed Aug 15, 2023
@MartinJohns
Copy link
Contributor

although I'm not really sure why, as the constant's type should be taken from its value.

For performance reasons only identifiers with type annotations are analyzed.

@Asaf-S
Copy link
Author

Asaf-S commented Aug 15, 2023

although I'm not really sure why, as the constant's type should be taken from its value.

For performance reasons only identifiers with type annotations are analyzed.

Make sense, thx!

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