Skip to content

Assertion functions do not work with arrow functions #52293

Closed
@ehmicky

Description

@ehmicky

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions