Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambient declaration styles and modules

There are people that are writing Backbone applications using a Backbone.d.ts. There are two use cases I want to discuss.

  1. Creating backbone applications with modules using an AMD loader (or CommonJS I suppose as well)
  2. Creating backbone applications using plain JS

For those in camp 1, it is necessary that the backbone module be defined as external so that the module is able to be imported and included in the define() wrapper.

For those in camp 2, it is necessary that the backbone module be defined as internal module in order to use the intellisense and not require the use of an import statement / define() wrapper.

Question: Is there some other way to define the module so that it can be used in both cases?


I don't really want to have to create a fork just so that you can have either

// required for those using import (1)
declare module "Backbone" { 

or

// required for those not using import (2) and backbone already exists in the global scope
declare module Backbone {

and still be able to get along with your code/intellisense.

like image 511
ryan Avatar asked Oct 05 '22 22:10

ryan


2 Answers

This can't be done using just one .d.ts file because the compiler needs to know what kind of module system you're using for backbone in order to know what kind of code to generate. You could be mixing and matching internal and external modules within a single file and there's no way to guess correctly which you were using for backbone in particular.

You might be able to use interface declarations to make it so that you can declare the majority of stuff in exactly one place and have a "backbone-internal.d.ts" and "backbone-external.d.ts" file that referenced that common declaration file, but the extent to which you could do that would depend on exactly what the surface area of the API looked like.

TL;DR: Camp 1 and Camp 2 are not mutually exclusive, even within the same file.

like image 118
Ryan Cavanaugh Avatar answered Oct 10 '22 02:10

Ryan Cavanaugh


I have raised this with the TypeScript team and they have opened a work item.

I proposed that if you write all your modules using the...

module MyModule {
}

...wrapper, if you send the module flag to the compiler it should strip the module declaration if it matches the file name. That way you could write one module that could be compiled to work on web, CommonJS or AMD without modification.

The original discussion is here, and it has been moved into a work item:

http://typescript.codeplex.com/discussions/401397

like image 45
Fenton Avatar answered Oct 10 '22 01:10

Fenton