Skip to content

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

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
phryneas opened this issue Feb 7, 2022 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: Error Messages The issue relates to error messaging Suggestion An idea for TypeScript

Comments

@phryneas
Copy link

phryneas commented Feb 7, 2022

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.

@phryneas phryneas changed the title Assertion Functions cannot assert over private properties. Assertion Functions cannot assert over private properties - unnecessary/wrong error message? Feb 7, 2022
@nmain
Copy link

nmain commented Feb 7, 2022

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.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Feb 7, 2022
@RyanCavanaugh
Copy link
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
Copy link
Contributor

@nmain #36548

@DanielRosenwasser DanielRosenwasser added the Domain: Error Messages The issue relates to error messaging label Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: Error Messages The issue relates to error messaging Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants