简体   繁体   English

仅使用环境定义导入TypeScript模块以在amd中使用

[英]Import TypeScript module using only ambient definition for use in amd

I have a module that depends on Backbone . 我有一个依赖Backbone的模块。 I have a backbone.d.ts definition but TypeScript doesn't seem to want to compile my module unless my 我有一个backbone.d.ts定义但TypeScript似乎不想编译我的模块,除非我的

import Backbone = module("backbone")

actually points to a valid backbone module as opposed to a definition file. 实际上指向有效的主干模块而不是定义文件。 I am using AMD loaded modules and have a requirejs shim defined for backbone. 我正在使用AMD加载的模块,并为骨干网定义了一个requirejs垫片。

Is there a workaround besides creating a phoney backbone.ts module definition? 除了创建一个虚假的backbone.ts模块定义之外,还有解决方法吗?

Update: A side effect of the solution is that code such as this no longer works because the module no longer exists. 更新:解决方案的副作用是此类代码不再有效,因为模块不再存在。 It needs to exist because of the requirejs shim. 它需要存在因为requirejs垫片。 The only workaround I know of is to have two .d.ts files. 我所知道的唯一解决方法是拥有两个.d.ts文件。 One for the file using backbone as an import that does not include the declare module bit. 一个用于使用骨干作为导入但不包含declare module位的文件。 The other for using a /// <reference that does include the declare module line. 另一个用于使用包含declare module行的/// <reference

/// <reference path="../dep/backbone/backbone.d.ts" />

interface IApi {
    version: number;
    Events: Backbone.Events;
}

The TypeScript language has changed a fair bit since this original answer. 自从这个原始答案以来,TypeScript语言已经改变了很多。

For example, to import an external module you use require (my original answer had the old module keyword): 例如,要导入外部模块,请使用require (我的原始答案具有旧的module关键字):

Here is a common use case for importing backbone - using the type information from Definitely Typed: 以下是导入骨干的常见用例 - 使用来自绝对类型的类型信息:

/// <reference path="scripts/typings/backbone/backbone.d.ts" />

import bb = require('backbone');

Within the type definition, the backbone module is declared for you, which is what allows the import to be valid: 在类型定义中,为您声明了主干模块,这使得导入有效:

//... lots of code and then...

declare module "backbone" {
    export = Backbone;
}

So the original question could be resolved using... 所以原来的问题可以用......来解决

/// <reference path="scripts/typings/backbone/backbone.d.ts" />

import bb = require('backbone');

interface IApi {
    version: number;
    Events: bb.Events;
}

class Api implements IApi {
    public version = 1;
    public Events: bb.Events = null;
}

For this code example, this is all that is required - but more often you will want the backbone library loaded at runtime... you can use the (officially experimental) amd-dependency comment to cause the generated define function call to include backbone. 对于这个代码示例,这就是所需要的 - 但更多时候您会希望在运行时加载主干库...您可以使用(官方实验性的) amd-dependency注释来使生成的define函数调用包含主干。

/// <reference path="scripts/typings/backbone/backbone.d.ts" />
/// <amd-dependency path="backbone" />

import bb = require('backbone');

interface IApi {
    version: number;
    Events: bb.Events;
}

class Api implements IApi {
    public version = 1;
    public Events: bb.Events = null;
}

There is actually another way to handle in : 实际上还有另一种方法来处理

  1. Clone the DefinitelyTyped Github repository. 克隆DefinitelyTyped Github存储库。 It contains the jquery.d.ts , backbone.d.ts and alot of other definition files. 它包含jquery.d.tsbackbone.d.ts和许多其他定义文件。

  2. Link the definition files to your myfile.ts file: 将定义文件链接到myfile.ts文件:
    /// <reference path="DefinitelyTyped/requirejs/require.d.ts" />
    /// <reference path="DefinitelyTyped/jquery/jquery.d.ts" />

  3. Add an amd dependency to the javascript library: 向javascript库添加amd依赖项:
    /// <amd-dependency path="jquery"/>

  4. To use $ inside your myfile.ts file you can now call require: 要在myfile.ts文件中使用$ ,您现在可以调用require:
    var $ = require("jquery");

Full version of myfile.ts : 完整版myfile.ts

/// <reference path="DefinitelyTyped/requirejs/require.d.ts" />
/// <reference path="DefinitelyTyped/jquery/jquery.d.ts" />
/// <amd-dependency path="jquery"/>
var $ = require("jquery");

export function helloWorld() {
  $("<div>Hello World</div").appendTo(document.body);
}

If you run tsc --module amd myfile.ts you will get the following javascript code: 如果运行tsc --module amd myfile.ts您将获得以下javascript代码:

define(["require", "exports", "jquery"], function(require, exports) {
    var $ = require("jquery");

    function helloWorld() {
        $("<div>Hello World</div").appendTo(document.body);
    }
    exports.helloWorld = helloWorld;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM