You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow .d.ts files to represent anonymous class expressions with private members #36060
changed the title [-]False error TS4094: ... exported class expression may not be private or protected.[/-][+]Allow .d.ts files to represent anonymous class expressions with `private` members[/+]on Jan 8, 2020
This seems like it would be a way to annotate classes that have computed properties when using isolated declarations. Right now the only way to type this
exportclassFooimplementsIterable<number>{[Symbol.iterator]: ()=>{// ^ Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.ts(9038)
...
}}
The simplest workaround is to use a convention like prefixing members with _ (one underscore) for protected, and prefixing members with __ (two underscores) for private, abandoning protected, private, and #private altogether.
This works:
typeConstructor<T=object,Aextendsany[]=any[],Static={}>=(new(...a: A)=>T)&Static/** * Members prefixed with __ are private, don't touch or you're a gonner! */functionCoolMixin(Base: Constructor<HTMLElement>){returnclassCoolClassextendsBase{__foo=123meth(other: CoolClass){console.log(other.__foo)}}}classOneextendsCoolMixin(HTMLElement){}customElements.define('x-one',One)constone=newOne()classTwoextendsCoolMixin(HTMLElement){}customElements.define('x-two',Two)consttwo=newTwotwo.meth(one)
For now, I use underscore conventions because that's easy! It does mean that the "protected" and "private" members are technically public, visible for IDE autocompletion in contexts when they otherwise wouldn't be, which is not ideal, but the visible underscores should be enough of a clue, especially if the convention is well documented.
Another workaround, for private members, it to use WeakMaps, but that's a bit more cumbersome than the simple underscore convention.
Activity
[-]False error TS4094: ... exported class expression may not be private or protected.[/-][+]Allow .d.ts files to represent anonymous class expressions with `private` members[/+]RyanCavanaugh commentedon Jan 8, 2020
Wouldn't it be cool if this had some text in it?
trusktr commentedon Jan 20, 2020
It doesn't need, the other issue has details, but it is closed. I just think that this is a valid problem that is faced much too often by TS devs.
web-padawan commentedon Jan 22, 2020
Related to #17293 (actually the same) - see #17293 (comment)
Related to #17744 - see my comment explaining why this is important #17744 (comment)
RonnieLincoln commentedon Aug 28, 2022
any process?
Encountered this issue when trying Builder Pattern.
MichaelMitchell-at commentedon May 26, 2024
This seems like it would be a way to annotate classes that have computed properties when using isolated declarations. Right now the only way to type this
is to do something like
which is pretty verbose and doesn't quite have the exact same semantics in some cases.
If anonymous class types were allowed then we could perhaps do something like
cc Titian Cernicova-Dragomir (@dragomirtitian) Wesley Wigham (@weswigham)
[FIX] ts: fix compiling of `.d.ts` files
[FIX] ts: fix compiling of `.d.ts` files
[FIX] ts: fix compiling of `.d.ts` files
[FIX] ts: fix compiling of `.d.ts` files
[FIX] ts: fix compiling of `.d.ts` files
trusktr commentedon Oct 13, 2025
I thought I'd try to be a little bit cool, so I added some text for some context. 😎😄
A notable use case for this is support for class-factory mixins.
This code,
currently produces the type error
playground
Note that using the newer
#hashtagprivate syntax,does not have a type error!
playground
However! Mixins that use
#hashtagprivate fields will experience runtime errors in JavaScript. This code,throws the error
playground
The simplest workaround is to use a convention like prefixing members with
_(one underscore) for protected, and prefixing members with__(two underscores) for private, abandoningprotected,private, and#privatealtogether.This works:
playground
For now, I use underscore conventions because that's easy! It does mean that the "protected" and "private" members are technically public, visible for IDE autocompletion in contexts when they otherwise wouldn't be, which is not ideal, but the visible underscores should be enough of a clue, especially if the convention is well documented.
Another workaround, for private members, it to use WeakMaps, but that's a bit more cumbersome than the simple underscore convention.
MichaelMitchell-at commentedon Jan 13, 2026
Isolated declarations was later extended to support the case I brought up. My previous comment can be disregarded.