Skip to content
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

Visibility of multiple same namespace in a .ts file #6822

Closed
tianyuanzhonglu opened this issue Feb 2, 2016 · 9 comments
Closed

Visibility of multiple same namespace in a .ts file #6822

tianyuanzhonglu opened this issue Feb 2, 2016 · 9 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@tianyuanzhonglu
Copy link

tsc 1.8.0 beta

demo.ts

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) {

    }
}   

}

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.

demo.ts

namespace NS_APP {

**export** class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

}

namespace NS_APP {

class Demo {
    constructor(private greeter: Greeter) {

    }
}   

}

My question, can Typescript support 'partial' namespace in one or multiple file?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 2, 2016

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 mhegazy closed this as completed Feb 2, 2016
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Feb 2, 2016
@tianyuanzhonglu
Copy link
Author

@mhegazy does multiple same namespace declared in one file merge? I test, not merged.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 2, 2016

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

@tianyuanzhonglu
Copy link
Author

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 = {}));

@mhegazy
Copy link
Contributor

mhegazy commented Feb 2, 2016

export mean visible for outside caller, it's not what I want.

export means visible to the anything outside this declaration. this applies merged namespaces, as well as any of their users.

@tianyuanzhonglu
Copy link
Author

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) {

    }
}   

}

@mhegazy
Copy link
Contributor

mhegazy commented Feb 2, 2016

namespace N {
    var x;
    export var y;

    x; // OK
    y; // OK
}

N.x // Error
N.y; // OK

namespace N {
    x; // Error
    y; // OK
}   

@tianyuanzhonglu
Copy link
Author

OK, I got.
Thank you very much!

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
}   

@fis-cz
Copy link

fis-cz commented May 13, 2017

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.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants