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
Create better guide on how to structure definitions #1562
Re: namespacing. What do you think about all the existing typings which pollute the Global namespace? Should there be a piece of work to namespace them into modules? It would probably affect lots of people. But it seems inconsistent to insist all new typings are namespaced when majority of the existing ones aren't.
The way people keep dumping interfaces in the global namespace will break at some point, probably on some helper definitions (like every graphics library will have it's own Rectangle or Point).
Ideally every definition has it's own module. Maybe only a CamelCase module as namespace and the import-compatible alias.
I personally also don't like how some definitions declare global var's, I guess it is for browser globals? I had some problems with those before and I usually end up editing and removing the global var.
No sure if this works for all modules out there (single export overload merging fun 😄); but that is what this thing would show.
Side note: if we'd decide break lots of peoples code it should happen together with any other breaking changes that have been mentioned (file/folder name cleanup and some other stuff, see Doc etc).
My guess is that all definitions probably declare global var's - jQuery declaring $ and jQuery / Knockout declaring ko etc. And unless the browser world undergos some kind of revolution that will presumably always be the case!
I think namespacing the existing typings is totally worth doing - couple of points:.
I think this work should be done in a separate branch. All typings namespaced and then merged up so everything breaks together (if you see what I mean). I guess this fits with what you were saying about:
" it should happen together with any other breaking changes that have been mentioned"
Agreed: This merging business is your (and @Igorbek's) expertise.
Agreed. Would be an epic work (> 300 defs). 😄
But regardless of repo-wide sweep-implementation, we can already make a best-practice guide and some idealised demo definitions.
We could opt to put these in their own repo (as some .d.ts and some markdown or whatever), then build a static site for github-pages.
If we get traction I could do some ground work: like making a list of all the variations in module exports I encounter on npm (crazy stuff). We can then fill them in with ideal definitions structures (modules vs statics vs interfaces vs classes etc etc).
I wish we could use a module to prevent it from colliding with other types, but I've struggled with doing this in my experience. Let's take a simple foo module that contains a single class, Bar:
declare module Foo{/** * Description of Bar class */classBar{// members}}
Now, let's allow people to require it:
declare module 'foo'{varfoo: Foo;// Type reference cannot refer to container 'Foo'.export=foo;}
Hmm... how do we make the foo module assignable? I suppose we could do this:
declare module Foo{interfaceFoo{Bar: typeofBar;}/** * Description of Bar class */classBar{// members}}
Oh, great! But now we've lost our Intellisense when we write:
varfoo=require('foo');foo.Bar// Intellisense lost
Let's try a different approach by stuffing everything into the require-able module directly:
declare module 'foo'{/** * Description of Bar class */classBar{// members}}
This works perfectly, even if you introduce another module named baz:
We're back on track, but I've run into a scenario with Electron where they have separate modules for the main process than the renderer process. If you're in one context, you don't have access to modules in the other context and vice versa; however, sometimes those modules return instances of a class in an inaccessible module.
A clear guide would be extremely useful to solve issues like this. It's taken me so long to get where I am now and I still feel like I haven't mastered it.
For the record, I don't think you should be allowed to pollute the global namespace with anything other than your module name in UpperCamelCase as well as any require-able modules. People can declare $ for jQuery and the like in a common file.
Activity
johnnyreilly commentedon Jan 18, 2014
Sounds like some good ideas there.
Re: namespacing. What do you think about all the existing typings which pollute the Global namespace? Should there be a piece of work to namespace them into modules? It would probably affect lots of people. But it seems inconsistent to insist all new typings are namespaced when majority of the existing ones aren't.
Bartvds commentedon Jan 18, 2014
The way people keep dumping interfaces in the global namespace will break at some point, probably on some helper definitions (like every graphics library will have it's own Rectangle or Point).
Ideally every definition has it's own module. Maybe only a CamelCase module as namespace and the import-compatible alias.
I personally also don't like how some definitions declare global
var's, I guess it is for browser globals? I had some problems with those before and I usually end up editing and removing the global var.No sure if this works for all modules out there (single export overload merging fun 😄); but that is what this thing would show.
Side note: if we'd decide break lots of peoples code it should happen together with any other breaking changes that have been mentioned (file/folder name cleanup and some other stuff, see Doc etc).
johnnyreilly commentedon Jan 18, 2014
My guess is that all definitions probably declare global
var's - jQuery declaring$andjQuery/ Knockout declaringkoetc. And unless the browser world undergos some kind of revolution that will presumably always be the case!I think namespacing the existing typings is totally worth doing - couple of points:.
Bartvds commentedon Jan 18, 2014
I made a separate issue for the global
var: #1564But regardless of repo-wide sweep-implementation, we can already make a best-practice guide and some idealised demo definitions.
We could opt to put these in their own repo (as some
.d.tsand some markdown or whatever), then build a static site for github-pages.If we get traction I could do some ground work: like making a list of all the variations in module exports I encounter on npm (crazy stuff). We can then fill them in with ideal definitions structures (modules vs statics vs interfaces vs classes etc etc).
Let's see what others think about this.
Bartvds commentedon Mar 25, 2014
Some really good info on this was added to the TypeScript wiki recently:
https://typescript.codeplex.com/wikipage?title=Writing%20Definition%20%28.d.ts%29%20Files
(I updated our wiki to link there too)
johnnyreilly commentedon Mar 25, 2014
Thanks for sharing @Bartvds !
jednano commentedon May 6, 2015
I wish we could use a module to prevent it from colliding with other types, but I've struggled with doing this in my experience. Let's take a simple
foomodule that contains a single class,Bar:Now, let's allow people to require it:
Hmm... how do we make the
foomodule assignable? I suppose we could do this:Oh, great! But now we've lost our Intellisense when we write:
Let's try a different approach by stuffing everything into the require-able module directly:
This works perfectly, even if you introduce another module named
baz:We're back on track, but I've run into a scenario with Electron where they have separate modules for the main process than the renderer process. If you're in one context, you don't have access to modules in the other context and vice versa; however, sometimes those modules return instances of a class in an inaccessible module.
A clear guide would be extremely useful to solve issues like this. It's taken me so long to get where I am now and I still feel like I haven't mastered it.
jednano commentedon May 6, 2015
For the record, I don't think you should be allowed to pollute the global namespace with anything other than your module name in UpperCamelCase as well as any require-able modules. People can declare
$for jQuery and the like in a common file.