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(){}
Warren Buckley
04/07/2024, 7:45 PM
Curious to know what I should do/use
m
Markus Johansson
04/08/2024, 6:25 AM
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
Niels Lyngsø
04/08/2024, 7:09 AM
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
Warren Buckley
04/08/2024, 7:54 AM
Ah interesting point
Warren Buckley
04/08/2024, 7:54 AM
Yeh had notice the mix in the codebase and had assumed different team members preferences.
Warren Buckley
04/08/2024, 7:55 AM
I need to bookmark this comment somehow, as that’s really useful to know