Should I use private or # in my code ?
# package-development
w
I know I could probably Google this, but wanted to know/hear the opinion from you clever lot. With Typescript should I be using
#openMyThing(){}
or
private openMyThing(){}
Curious to know what I should do/use
m
I would go with #openThing() where you don't have a specific need for something else as this is more "true" to ECMAScript, not sure about the state of it but I think that this is what going to be a private class field in the future (or might already be). I also think that there is a difference in inheritance, the typescript-specific "private" accessor will be accessible from sub-classes but not the one with #openThing. I think you'll need to Google to get the real truth here 😄 Haha sorry 😄
n
Exactly what Markus wrote. I would say the point is that # is a JS native private, it is actually private.. where the TS private is just a compile time check, but any one could access/manipulate the variable. like:
(this as any).myPrivate = 'you got hacked';
But notice in Backoffice land/ aka. Lit land. there is a thing to be aware about, cause if you like your private property to be reactive, with the Lit-decorators, either @property() or @state() then they cannot be native Private(#) cause then Lit cant bind to it. so this is one of the reasons why you will see a mix in our examples and the core code. 🙂
w
Ah interesting point
Yeh had notice the mix in the codebase and had assumed different team members preferences.
I need to bookmark this comment somehow, as that’s really useful to know
2 Views