Skip to content

Suggestion: internal modifier #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mbebenita opened this issue Oct 14, 2014 · 16 comments
Closed

Suggestion: internal modifier #892

mbebenita opened this issue Oct 14, 2014 · 16 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@mbebenita
Copy link

Currently the spec reads: Protected property members can be accessed only within their declaring class and classes derived from their declaring class, and a protected instance property member must be accessed through an instance of the enclosing class.

Relaxing the constraint, "member must be accessed through an instance of the enclosing class" to allow all classes derived from the declaring class as long as they are declared in the same module as the declaring class, would make it possible to have module internal access.

This is closer to how Java implements protected access, rather than C#. Are there plans to add an internal access modifier? If not, then this may be a way to alleviate the pain of encapsulating internal class state.

@danquirk
Copy link
Member

Edited the title to make this a suggestion for internal which we have not formally had yet but certainly discussed while considering protected. We don't intend to change protected to mean this but obviously there is a desire for additional accessibility modifiers. Some issues that surface while thinking about this are how to handle merged modules (internal and external).

@danquirk danquirk changed the title Allow access to protected members through all derived classes as long as they are in the same module as the declaring class. Suggestion: internal modifier Oct 14, 2014
@danquirk danquirk added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Oct 14, 2014
@AbubakerB
Copy link
Contributor

I was going to raise a suggestion about this, but i found this!
i think its a very important feature.

+1 for the suggestion!

@mreshadi
Copy link

I'm not sure if "internal" should be mixed with "protected" etc. But at the very least, it would be very useful to use "internal" instead of "export" for module level definitions so that they could be only accessed by members of the same module in other files.
Basically, module can then act almost like the "assembly" concept in .NET.

@pnewhook
Copy link

+1 for the internal modifier. I want to split my code across a few classes, but only a subset should be available to external consumers.

@SergioMorchon
Copy link

+1 for internal, specially when biulding reusable code!

@basarat
Copy link
Contributor

basarat commented Apr 16, 2015

Meanwhile "_" is your friend to ease API discovery

@SergioMorchon
Copy link

Maybe... but not for linters and SoC: there are already accessor modifiers, so prefixing _ would be redundant, like prefixing string variables strName which already has a type string.

Thanks @basarat

@danquirk danquirk mentioned this issue May 22, 2015
@gautamsi
Copy link

+1, I am working on large code base to port from c# and it would leak many methods to consuming code outside of my module.
may I suggest few options (for internal and external modules):

  • use tsconfig.josn list of files to allow use of internal modifiers
  • add a section in tsconfig.json to list those files. they can use each others internal modifier
  • use ///<reference like section with wildcard relative path on top of file which is declaring internal modifier, if wildcard matches the file you are working, you can use internal modifiers.

it may also be possible to ignore this during compilation and consider internal as public, but in generating definition file skip them.

@jezzgoodwin
Copy link

I have been working in typescript for a few months now, creating many modules and classes.

This is one of my most desired features.

+1

@benliddicott
Copy link

Related, #3854.

Really I think there is a basic problem with trying to be too "pure OO" with access modifiers. This is JavaScript! Any access modifier is ultimately going to be advisory since people can use the [] escape hatch to access your private members if they want to. And they will, just as soon as they encounter a use-case not covered by your class hierarchy.

Modifiers protected and private should not be treated like a chain and padlock, but as a sign saying "Danger - Qualified Personnel Only".

@SergioMorchon
Copy link

@benliddicott Yes, we know... But as TypeScript programmer (not JavaScript), it would be great.

It is known that it generates plain ECMAScript, and consumers will be able to hack it. But this is not the TypeScript purpose (obfuscate, protect etc.), but maintain readability and increase productivity.
Bytecode, MSIL, all of them are vulnerable even making "private" or "protected" or "internal" (etc.), becasue of i.e. reflection, perfecty legal!.

I think that that is not the point of view.

@m10s
Copy link

m10s commented Sep 11, 2015

+1 for internal

@apastuhov
Copy link

+1

@rbuckton
Copy link
Member

rbuckton commented Oct 7, 2015

My take on internal is that it should have the following behavior:

  • Available only on a class declaration/expression (for now)
  • A member may be marked with only one of public, protected, internal, or private (no protected internal, at least for now)
  • Members marked internal in a Source File are treated as public to any other Source File or Declaration File within a Program
  • Members marked internal in a Declaration File are treated as public to any other Declaration File (for use with sub-types across multiple declaration files) and private to any Source File within a Program

We would want to emit any internal members of a class out to a respective declaration file in the same way that we emit a private member. This would be used to prevent collisions in subclasses where the superclass is defined in a declaration file.

I'm not suggesting mixing internal with export at this time. For external modules, you can approximate this by just not exporting any internal API from the main module of your package. It may be useful to discuss supporting export internal for namespaces, however, as there's no other way to exclude these types.

@SergioMorchon
Copy link

@rbuckton looks nice :) thank you!

@mhegazy
Copy link
Contributor

mhegazy commented Oct 21, 2015

closing in favor of #5228

@mhegazy mhegazy closed this as completed Oct 21, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests