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

查看:19
本文介绍了仅使用环境定义导入 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 shim,它需要存在.我知道的唯一解决方法是有两个 .d.ts 文件.一种用于使用主干作为不包含 declare module 位的导入的文件.另一个使用 /// 包含 declare module 行.

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 的外部模块(我原来的答案是旧的 module 关键字):

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天全站免登陆