microsoft / microsoft/TypeScript

Unable to invoke an arrow function with an assertion predicate

Open
#56,147 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

arrow function, assertion, predicate, 2775

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://tsplay.dev/w8QLpN

💻 Code
type A = { x: number };

type B = A & { y: number };

const foo = (a: A): asserts a is B => {
  if ((a as B).y !== 0) throw TypeError();
  return undefined;
};

export const main = () => {
  const a: A = { x: 1 };
  // TypeError: Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
  foo(a);
};
🙁 Actual behavior

It looks like they goal of https://github.com/microsoft/TypeScript/pull/45952 was for assertion predicates to work for arrow functions, but this only handles the JSDoc case, which I assume TS treats as equivalent to directly annotating the entire function at once, i.e.:

 const foo: (a: A) => asserts a is B = (a) => {};

For functions with more named parameters, this annotation style quickly becomes unwieldy. There is no semantic difference between this annotation or the style from the repro:

const foo = (a: A): asserts a is B => {};

Is it possible to identify a top-level arrow function like this assigned to a const variable with annotated parameters and return as having an "explicit type," which was defined as the standard for being able to use an assertion predicate like this in @ahejlsberg's initial PR (https://github.com/microsoft/TypeScript/pull/32695)?

This style is increasingly common, and I think many developers (my self included) simply write a function like this, see it doesn't work, then choose not to use the feature, not knowing something like moving the annotation to the variable itself could have an effect.

🙂 Expected behavior

(see above)

Additional information about the issue

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript Playground repro and compare its arrow-function assertion predicate handling with the behavior described in PRs #45952 and #32695. Trace the explicit-type requirement for the const-bound arrow function, then validate the expected behavior with a focused compiler test covering the provided example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.