Skip to content

Assertion functions do not work with arrow functions #52293

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
ehmicky opened this issue Jan 18, 2023 · 2 comments
Closed

Assertion functions do not work with arrow functions #52293

ehmicky opened this issue Jan 18, 2023 · 2 comments

Comments

@ehmicky
Copy link

ehmicky commented Jan 18, 2023

Bug Report

Assertion functions do not work when using arrow functions.

🔎 Search Terms

ts2775, arrow functions, assertion functions

🕗 Version & Regression Information

Assertion functions were introduced in 3.7. The behavior has been the same since that version.
This is the behavior in every version I tried, and I reviewed the FAQ for entries about it.

⏯ Playground Link

Playground link with relevant code

💻 Code

function assertOne(val: unknown): asserts val is string {
  if (typeof val !== "string") {
    throw new Error("Not a string!");
  }
}

const assertTwo = (val: unknown): asserts val is string => {
  if (typeof val !== "string") {
    throw new Error("Not a string!");
  }
}

function main(str: unknown) {
  assertOne(str); // works
  assertTwo(str); // "Assertions require every name in the call target to be declared with an explicit type annotation. (ts2775)"
  return str;
}

🙁 Actual behavior

assertOne() works. assertTwo() (although nearly identical) shows the following type checking error:

Assertions require every name in the call target to be declared with an explicit type annotation. (ts2775)

🙂 Expected behavior

assertTwo() should not show any type checking error.

@xiBread
Copy link

xiBread commented Jan 18, 2023

Duplicate of #34523

@ehmicky
Copy link
Author

ehmicky commented Jan 18, 2023

Thanks @xiBread!
This is indeed a duplicate, so I will close.

@ehmicky ehmicky closed this as completed Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants