Skip to content

New asserts syntax fails with arrow functions #33602

Closed
@nimashoghi

Description

@nimashoghi

The new assertions in control flow analysis (see #32695) feature fails to work when the assert function is defined as an arrow function (i.e. defined using const or let).

TypeScript Version: 3.7.0-dev.20190925

Search Terms:
Assertions in control flow analysis

Code

const assert0 = (input: unknown): asserts input => {}
function assert1(input: unknown): asserts input {}

declare const assert2: (input: unknown) => asserts input
declare function assert3(input: unknown): asserts input

const inputFunction = (input: number) => {}

const test = (x: unknown, y: unknown, z: unknown, w: unknown) => {
    assert0(typeof x === "number")
    x // VSCode intellisense says the type of x is still unknown here
    inputFunction(x)

    assert1(typeof y === "number")
    y // VSCode intellisense says y is number, which is expected
    inputFunction(y)

    assert2(typeof z === "number")
    z // VSCode intellisense says z is number, which is expected
    inputFunction(z)

    assert3(typeof w === "number")
    w // VSCode intellisense says w is number, which is expected
    inputFunction(w)
}

Expected behavior:
x, y, z, w should all have the type number after the respective assertion.

Actual behavior:
x remains as unknown. This behavior is exhibited both in VSCode's type preview (see here: https://i.imgur.com/04uJTor.png) and as a compile error when we call inputFunction(x). The tsc error output is shown below:

index.ts:12:19 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'number'.

12     inputFunction(x)
                     ~


Found 1 error.

Playground Link:
Playground's "nightly" version doesn't support the asserts syntax.

Related Issues:
N/A

Activity

nimashoghi

nimashoghi commented on Sep 25, 2019

@nimashoghi
Author

@lppedd was playing around with this in Gitter and noticed that the following works:

const assert0: (input: any) => asserts input is number = (input: any): asserts input is number => {};
lppedd

lppedd commented on Sep 25, 2019

@lppedd

It seems that just explicitly specifying the type makes it work.

const assert0: (input: unknown) => asserts input = (input: unknown): asserts input => {};
jack-williams

jack-williams commented on Sep 25, 2019

@jack-williams
Collaborator

See related discussion here: #33580

typescript-bot

typescript-bot commented on Sep 28, 2019

@typescript-bot
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nimashoghi@jack-williams@RyanCavanaugh@lppedd@typescript-bot

        Issue actions

          New asserts syntax fails with arrow functions · Issue #33602 · microsoft/TypeScript