Skip to content

TS2775 when using arrow function for type predicate #45919

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
dckc opened this issue Sep 16, 2021 · 1 comment · Fixed by #45952
Closed

TS2775 when using arrow function for type predicate #45919

dckc opened this issue Sep 16, 2021 · 1 comment · Fixed by #45952
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Milestone

Comments

@dckc
Copy link

dckc commented Sep 16, 2021

Bug Report

Our shop prefers arrow function style and JSDoc annotations. I've got a type predicate that works as a function declaration but as an arrow function, I get 2775

🔎 Search Terms

  • arrow function
  • type predicate
  • TS2775

🕗 Version & Regression Information

Version 4.2.3 (and whatever version the playground runs)

  • This is the behavior in every version I tried

⏯ Playground Link

error

💻 Code

// @ts-check

/**
 * @typedef {{ legs: number }} Animal
 */

/**
 * @typedef { Animal & { says: 'bark'}} Dog
 */

/**
 * @param {Animal} specimen
 * @returns { asserts specimen is Dog }
 */
export const canine = specimen => {
  if (/** @type { Dog } */ (specimen).says !== 'bark') throw TypeError();
  return undefined;
};

/** @param { string[] } argv */
export const main = argv => {
  /** @type { Animal } */
  const specimen1 = JSON.parse(argv[3]);
  canine(specimen1);
  console.log(specimen1.says);
};

🙁 Actual behavior

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

🙂 Expected behavior

same as when I change the arrow function to a function declaration:

no error

@ilogico
Copy link

ilogico commented Sep 17, 2021

Looks like you can work around it by using a @type annotation instead: playground

@andrewbranch andrewbranch added Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Sep 17, 2021
@andrewbranch andrewbranch added this to the Backlog milestone Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants