Skip to content

Create better guide on how to structure definitions #1562

Open

Description

@Bartvds

The contribution guide on 'namespacing' seems outdated: https://github.com/borisyankov/DefinitelyTyped/wiki/How-to-contribute#namespacing

I looked around the repo and the structure of the declarations is unclear.

Could be cool to add a markdown-document and/or some example files to show how to make a really good definition file.

Should cover some practical stuff:

  • support current TypeScript version ~0.9.5 (or 1.0-ish?)
  • work with import
  • declare "module"
  • no global-namespace pollution
  • work for node modules
  • work in browser code
  • ... ?

Activity

  1. johnnyreilly commented on Jan 18, 2014

    @johnnyreilly
    Member

    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.

  2. Bartvds commented on Jan 18, 2014

    @Bartvds
    CollaboratorAuthor

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

  3. johnnyreilly commented on Jan 18, 2014

    @johnnyreilly
    Member

    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:.

    1. There's currently a bug with Declaration Merging for the Visual Studio tooling with TypeScript 0.9.5. It would be good to wait until this issue is resolved (presumably with TS 1.0 RC) before starting this work.
    2. 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"

  4. Bartvds commented on Jan 18, 2014

    @Bartvds
    CollaboratorAuthor

    I made a separate issue for the global var: #1564

    1. Agreed: This merging business is your (and @Igorbek's) expertise.
    2. 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).

    Let's see what others think about this.

  5. Bartvds commented on Mar 25, 2014

    @Bartvds
    CollaboratorAuthor

    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)

  6. johnnyreilly commented on Mar 25, 2014

    @johnnyreilly
    Member

    Thanks for sharing @Bartvds !

  7. jednano commented on May 6, 2015

    @jednano
    Contributor

    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
       */
      class Bar {
        // members
      }
    }

    Now, let's allow people to require it:

    declare module 'foo' {
      var foo: 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 {
      interface Foo {
        Bar: typeof Bar;
      }
      /**
       * Description of Bar class
       */
      class Bar {
        // members
      }
    }

    Oh, great! But now we've lost our Intellisense when we write:

    var foo = 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
       */
      class Bar {
        // members
      }
    }

    This works perfectly, even if you introduce another module named baz:

    declare module 'baz' {
      import foo = require('foo');
      function qux(): foo.Bar;
    }

    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.

  8. jednano commented on May 6, 2015

    @jednano
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions