Skip to content

Unable to invoke an arrow function with an assertion predicate #56147

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

Open
ssalbdivad opened this issue Oct 18, 2023 · 2 comments
Open

Unable to invoke an arrow function with an assertion predicate #56147

ssalbdivad opened this issue Oct 18, 2023 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@ssalbdivad
Copy link

ssalbdivad commented Oct 18, 2023

πŸ”Ž 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 #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 (#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

@fatcerberus
Copy link

Unfortunately, it is indeed a design limitation that foo must be explicitly annotated. Annotating the arrow function itself isn't enough - the compiler doesn't know the type of foo until type inference kicks in, which happens after the control flow graph is computed.

@ssalbdivad
Copy link
Author

@fatcerberus Do you know if there's a mechanism for this to be special-cased? It seems like it would be relatively simple to detect a top-level arrow function declaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants