Open
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]Assertion Functions cannot assert over private properties.[/-][+]Assertion Functions cannot assert over private properties - unnecessary/wrong error message?[/+]nmain commentedon Feb 7, 2022
The quick info on that function is a bit wonky:
Where the number
46
goes up as you edit the document.RyanCavanaugh commentedon Feb 7, 2022
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 commentedon Feb 7, 2022
@nmain #36548