仅使用环境定义导入TypeScript模块以在amd中使用 [英] Import TypeScript module using only ambient definition for use in amd

查看:102
本文介绍了仅使用环境定义导入TypeScript模块以在amd中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖于 Backbone 的模块。我有一个 backbone.d.ts 定义但TypeScript似乎不想编译我的模块,除非我

I have a module that depends on Backbone. I have a backbone.d.ts definition but TypeScript doesn't seem to want to compile my module unless my

import Backbone = module("backbone")

实际指向到有效的主干模块而不是定义文件。我正在使用AMD加载的模块,并为骨干定义了一个requirejs垫片。

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.

除了创建一个虚假的backbone.ts模块定义之外还有解决方法吗?

Is there a workaround besides creating a phoney backbone.ts module definition?

更新:解决方案的副作用是此类代码不再有效,因为模块已不存在。它需要存在因为requirejs垫片。我所知道的唯一解决方法是拥有两个.d.ts文件。一个用于使用骨干作为导入的文件,不包含声明模块位。另一个用于使用 ///< reference ,其中包含声明模块行。

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. The only workaround I know of is to have two .d.ts files. One for the file using backbone as an import that does not include the declare module bit. The other for using a /// <reference that does include the declare module line.

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

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


推荐答案

TypeScript语言已经改变了自这个原始答案以来的公平位。

The TypeScript language has changed a fair bit since this original answer.

例如,要导入外部模块,请使用 require (我的原始答案)有旧的模块关键字):

For example, to import an external module you use require (my original answer had the old module keyword):

这是导入骨干网的常见用例 - 使用来自的类型信息绝对键入:

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;
}

对于这个代码示例,这就是所需要的 - 但更常见的是将希望在运行时加载骨干库...你可以使用(官方实验) amd-dependency 注释来导致生成的 define 函数调用包含骨干。

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.

/// <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;
}

这篇关于仅使用环境定义导入TypeScript模块以在amd中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆