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

allow type declarations inside objects #8308

Closed
zpdDG4gta8XKpMCd opened this issue Apr 26, 2016 · 9 comments
Closed

allow type declarations inside objects #8308

zpdDG4gta8XKpMCd opened this issue Apr 26, 2016 · 9 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

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Apr 26, 2016

interface W<X, Y, Z> { x: X; y: Y; z: Z; }

function toModule<X, Y, Z>()  {
     type $W = W<X, Y, Z>;
     return {
        W: $W // <-- wish could do that
     };
}

const m = toModule<string, number, boolean>();
const w : m.W = { // <-- wish could do that
   x: '',
   y: 0,
   z: false
};
@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2016

so namespace expressions, a la class expressions?

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Apr 26, 2016
@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Apr 26, 2016

please allow either of the following:

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2016

here is a sample:

function toModule<X, Y, Z>()  {
     return namespace {
        export interface W { x: X; y: Y; z: Z; }
    }
}

const m = toModule<string, number, boolean>();
const w : m.W = { // <-- wish could do that
   x: '',
   y: 0,
   z: false
};

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2016

both approach has issues with the current implementations that assumes an expression can only be a value, and not a namespace. we will need a proposal for this one.

@mhegazy mhegazy added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Apr 26, 2016
@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Apr 26, 2016

Workaround

This is the workaround that I have to use now:

interface W<X, Y, Z> { x: X; y: Y; z: Z; }

function toModule<X, Y, Z>()  {
     const $W : W<X, Y, Z>;
     return {
        W: $W
     };
}

const m = toModule<string, number, boolean>();
const w : typeof m.W = {
   x: '',
   y: 0,
   z: false
};

@aluanhaddad
Copy link
Contributor

👍

@zpdDG4gta8XKpMCd
Copy link
Author

@mhegazy

here is a sample:

that would be super great

@zpdDG4gta8XKpMCd
Copy link
Author

I am afraid that alone returning a namespace isn't enough. It needs to be also passed as an argument and taken as a parameter. In order to declare it it has to have a type. Do namespaces have a type per se? Doubt so. Anyway, I believe that the difference between namespaces and objects is a made up. It would be great to see namepaces unified with objects because to JavaScript they are the same.

@zpdDG4gta8XKpMCd
Copy link
Author

closing in favor of #8358

@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
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

3 participants