-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Visibility of multiple same namespace in a .ts file #6822
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
Comments
namespace do merge across files. declarations within a namespace are not visible outside the block they are declared in unless they are marked as "export"ed. i think what you are looking for is an internal modifier, see #321. |
@mhegazy does multiple same namespace declared in one file merge? I test, not merged. |
namespaces do merge across files and within the same file. see more documentation in https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Declaration%20Merging.md#merging-namespaces |
export mean visible for outside caller, it's not what I want. namespace NS_APP {
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
}
namespace NS_APP {
class Demo {
constructor(private greeter: Greeter) {
}
}
} generate: var NS_APP;
(function (NS_APP) {
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
})(NS_APP || (NS_APP = {}));
var NS_APP;
(function (NS_APP) {
var Demo = (function () {
function Demo(greeter) {
this.greeter = greeter;
}
return Demo;
})();
})(NS_APP || (NS_APP = {})); I mean var NS_APP;
(function (NS_APP) {
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
var Demo = (function () {
function Demo(greeter) {
this.greeter = greeter;
}
return Demo;
})();
})(NS_APP || (NS_APP = {})); |
|
demo.ts
namespace NS_APP {
/* I want Greeter only visible in NS_APP */
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
/* Demo1 can see Greeter */
class Demo1 {
constructor(private greeter: Greeter) {
}
}
let demo = new Demo1(new Greeter('worl'));
}
namespace NS_APP {
/* Demo2 in same namespace(NS_APP) and same file, so it should see Greeter too */
class Demo2 {
constructor(private greeter: Greeter) {
}
}
} |
namespace N {
var x;
export var y;
x; // OK
y; // OK
}
N.x // Error
N.y; // OK
namespace N {
x; // Error
y; // OK
} |
OK, I got. namespace N {
var x;
export var y;
x; // OK
y; // OK
}
N.x // Error
N.y; // OK
namespace N {
x; // Error
y; // OK
}
namespace X {
N.y; // OK too, but I don't want
} |
It would be good it is optional to merge namespaces across different files. Especially when --out is specified. Will be very helpful in class-per-file code design. |
tsc 1.8.0 beta
tsc error prompt:
error TS2304: Cannot find name 'Greeter'.
I want
Greeter
to be private/protected for same namespace, if I use export token,Greeter
is public for namespace NS_APP.My question, can Typescript support 'partial' namespace in one or multiple file?
The text was updated successfully, but these errors were encountered: