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
Error while accessing Namespace level variables from multiple files defining the same namespace. #6457
Since I am extending the same namespace, I would expect that the parameterToShareBetweenFiles should be available in the secondfile.ts but the compiler throws
error TS2304: Cannot find name 'parameterToShareBetweenFiles'
I believe this is a Bug and needs to be fixed. Because the error doesn't come if the class definition for ChildClass2 is brought into the main.ts file as below. Does this mean that the entire namespace has to be defined in a single file? That is very inconvenient especially with multiple developers contributing to the same namespace.
namespace System.Parent.Level1Child{
var parameterToShareBetweenFiles: number = 0;
export class ChildClass1{
constructor(){
parameterToShareBetweenFiles += 1;
}
}
export class ChildClass2{
constructor(){
parameterToShareBetweenFiles += 1;
}
}
}
Thanks that works. The resulting javascript defines the namespace twice rather than optimizing the code into one block of code. Is this by design? This is not optimal.
Thanks for referring the issue #447. The problem with exporting the internal variables in the namespace is that they are exposed to code outside the namespace. In my third file namespaceclient.ts, I see that I am able to access and manipulate the parameterToShareBetweenFiles. This variable wasn't supposed to be accessible this way but only to classes which are with in the namespace.
Below are the contents of namespaceclient.ts
/// <reference path="secondfile" />
var classvar1 = new System.Parent.Level1Child.ChildClass1();
//this variable was not supposed to be accessed like this but TypeScript is allowing.
System.Parent.Level1Child.parameterToShareBetweenFiles += 1;
How can a developer protect the variables with in the namespace to be only accessible by code defined in the context of the namespace and not outside it?
TypeScript does not have an internal modifier (#5228) at the moment. keep in mind that namespaces are objects at the end of the day. and exported members are just properties on the object, so any one can access them.
Activity
mhegazy commentedon Jan 12, 2016
you need to export the variable to make it accessible across declarations.
seshualluvada commentedon Jan 12, 2016
Thanks that works. The resulting javascript defines the namespace twice rather than optimizing the code into one block of code. Is this by design? This is not optimal.
mhegazy commentedon Jan 12, 2016
it is by design. there are scoping issues in place that warrants different closures. you can read more about this in #447
seshualluvada commentedon Jan 12, 2016
Thanks for referring the issue #447. The problem with exporting the internal variables in the namespace is that they are exposed to code outside the namespace. In my third file
namespaceclient.ts, I see that I am able to access and manipulate theparameterToShareBetweenFiles. This variable wasn't supposed to be accessible this way but only to classes which are with in the namespace.Below are the contents of
namespaceclient.tsHow can a developer protect the variables with in the namespace to be only accessible by code defined in the context of the namespace and not outside it?
mhegazy commentedon Jan 12, 2016
TypeScript does not have an
internalmodifier (#5228) at the moment. keep in mind that namespaces are objects at the end of the day. and exported members are just properties on the object, so any one can access them.