Skip to content
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

asserts x is type doesn't work when using arrow functions #34523

Closed
mweststrate opened this issue Oct 16, 2019 · 6 comments
Closed

asserts x is type doesn't work when using arrow functions #34523

mweststrate opened this issue Oct 16, 2019 · 6 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@mweststrate
Copy link

mweststrate commented Oct 16, 2019

TypeScript Version: 3.7.0-dev.20191016

Search Terms: assertion signatures, asserts, asserts is type

Code

function isString1(val: any): asserts val is string {
    if (typeof val !== "string") throw "Nope"
} 

const isString2 = (val: any): asserts val is string => {
    if (typeof val !== "string") throw "Nope"
} 

const x: any = "3";

isString1(x);
isString2(x);

x.toUpperCase();

Expected behavior:

Compiles, infers that x is a string, both when using only isString1 or isString2

Actual behavior:

Compile error on isString2:

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

Playground Link: 1⃣ playground

Related Issues: #33743

Context

In principle the above problem is easy to work around, however, the result of it is when type checkers are stored on objects, the above error returns as is demonstrated in 2⃣ this playground. Note how the very same function fails when it is stored in an object, unless the type is provided explicitly.

@jcalz
Copy link
Contributor

jcalz commented Oct 17, 2019

This is (perhaps unfortunately) working as intended, as per #33622.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Oct 17, 2019
@mweststrate
Copy link
Author

Thanks for looking into it! Nothing I cannot work around in the end. Just felt surprising / inconsistent :)

bennypowers added a commit to open-wc/open-wc that referenced this issue May 6, 2020
TS casts the `required` assert function to a const, then pukes on the assertion
see microsoft/TypeScript#34523 (comment)
LarsDenBakker pushed a commit to open-wc/open-wc that referenced this issue May 7, 2020
TS casts the `required` assert function to a const, then pukes on the assertion
see microsoft/TypeScript#34523 (comment)
@pstrh
Copy link

pstrh commented Sep 29, 2020

As mentioned here (#33622 (comment)) a workaround is:

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

const x: unknown = "3";

isString2(x);

x.toUpperCase();

douglasnaphas referenced this issue in cdk-turnkey/sredirect Feb 6, 2022
@Pyrolistical
Copy link

A better error message on this design limitation would have been nice instead of the confusing TS 2775

@voltrevo
Copy link

voltrevo commented Oct 13, 2022

This is (perhaps unfortunately) working as intended, as per #33622.

@jcalz Unfortunate indeed. Do you know the rationale behind this intention? What would be the complication of allowing arrow functions to have type assertions?

@sezanzeb
Copy link

A better error message on this design limitation would have been nice instead of the confusing TS 2775

I created a new issue for that: #53450

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

7 participants