Skip to content

Assertion Functions cannot assert over private properties - unnecessary/wrong error message? #47778

Open
@phryneas

Description

@phryneas

Assertion Function "Private identifiers are not allowed outside class bodies."

Bug Report

🔎 Search Terms

assertion function, asserts, #, private members, Private identifiers are not allowed outside class bodies.

🕗 Version & Regression Information

This happens in 4.0.5, 4.4.3, 4.5.4 and the latest nightlies - so probably in all TS versions that support assertion functions and private class members.

⏯ Playground Link

A playground showcasing the problem

💻 Code

class Test {
    #ready: boolean = false;

    #assertReady(): asserts this is {
        // Private identifiers are not allowed outside class bodies.(18016)
        #ready: true // this is working perfectly fine though...
    } {
        if (this.#ready) {
            throw new Error("not ready")
        }
    }
}

🙁 Actual behavior

The code above works 100% as expected (the assertion function also narrows the type of this.#ready down to true correctly), but TS displays an error message:

Private identifiers are not allowed outside class bodies.(18016)

🙂 Expected behavior

The error message should be removed - this seems to work absolutely fine as it is. The error message seems to be a red herring.

Activity

changed the title [-]Assertion Functions cannot assert over private properties.[/-] [+]Assertion Functions cannot assert over private properties - unnecessary/wrong error message?[/+] on Feb 7, 2022
nmain

nmain commented on Feb 7, 2022

@nmain

The quick info on that function is a bit wonky:

(method) Test.#assertReady(): asserts this is {
    "__#46@#ready": true;
}

Where the number 46 goes up as you edit the document.

added
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
SuggestionAn idea for TypeScript
on Feb 7, 2022
RyanCavanaugh

RyanCavanaugh commented on Feb 7, 2022

@RyanCavanaugh
Member

The error is intentional. While it may appear to work, this hasn't been designed or tested at all, so there might be dragons lurking, hard to say.

MartinJohns

MartinJohns commented on Feb 7, 2022

@MartinJohns
Contributor
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

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: Error MessagesThe issue relates to error messagingSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @DanielRosenwasser@phryneas@MartinJohns@RyanCavanaugh@nmain

        Issue actions

          Assertion Functions cannot assert over private properties - unnecessary/wrong error message? · Issue #47778 · microsoft/TypeScript