Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a backbone.d.ts

I can't seem to get any import to work when the module isn't defined as a string. What is going on?

test.ts

import b = module('Backbone')

Does not work: backbone.d.ts

declare module Backbone {

    export class Events {
    ...

Works: backbone.d.ts

declare module "Backbone" {

    export class Events {
    ...

Edit 1:

FYI From 10.1.4

An AmbientModuleIdentification with a StringLiteral declares an external module. This type of declaration is permitted only in the global module. The StringLiteral must specify a top-level external module name. Relative external module names are not permitted

I don't understand how it's useful to not specify it as the string literal format as found here and here. It works if you use ///<reference... without a string literal module but I'm trying to generate AMD modules that depend on these libraries so I need the import to work. Am I the minority and I have to go and modify each .d.ts to be the string literal version?

Edit 2:

Declaring a module using a string literal requires that your import is an exact match if that string literal. You can no longer use relative or absolute paths to the location of this module/module definition even if it is not located in the same directory as the file trying to import it. This makes it that a ///<reference and an import are required but produces a module with tsc --module AMD that was exactly looking for (path to module is as dictated by the module string literal "Backbone").

For example.

+- dep/
   |- backbone.d.ts
|- test.ts

backbone.d.ts:

declare module "Backbone" {
    export class Events {

Works: test.ts:

///<reference path="../dep/backbone.d.ts" />
import b = module('Backbone')

// generates
// define(["require", "exports", 'Backbone']

Does not work: test.ts:

import b = module('./dep/Backbone')

Note that this works as well...

declare module "libs/Backbone" {
...
///<reference path="dep/backbone.d.ts" />
import b = module('libs/Backbone')
...
// generates
define(["require", "exports", 'libs/Backbone']
like image 958
ryan Avatar asked Nov 12 '22 19:11

ryan


1 Answers

When you write this:

declare module Backbone {

It means you have already a module which is in the global scope (so you can immeadiately use it, no import is required). But when you write this:

declare module "Backbone" {

It means you specify how the imported module (import ... = module("...")) will look.

like image 172
Razem Avatar answered Nov 15 '22 08:11

Razem