4 ms·
You can make the type itself public without making the fields public. This means someone could hypothetically create one of your structs without using the "New"
by azurelake 5y ago
You can make the type itself public without making the fields public. This means someone could hypothetically create one of your structs without using the "New" function you provide, but it's not an issue in practice. And if you really want to avoid exporting your struct at all, you can always just return an interface.
- eyelidlessness 5y ago> And if you really want to avoid exporting your struct at all, you can always just return an interface. This is what I think most TypeScript libraries should be doing, but every bit of inertia makes it unlikely. In all honesty I think type systems should be designed around contracts/protocols and make concrete types private by default. Sure it’s a lot of ceremony, but it’s less ceremony than human communication in emails and issue threads to clarify that undocumented features are undocumented on purpose.