使用TypeScript中已声明模块的类型 [英] Use typings from a declared module in TypeScript
问题描述
我正在阅读DurandalJS.com网站上的Durandal入门指南,但我尝试使用TypeScript进行操作。最后一个演示(雷尼尔山演示)有一个模块,其代码如下:
I'm going through the Durandal "get started" guide on the DurandalJS.com site, except I'm trying to do it with TypeScript. The last demo (the Mount Rainier one) has a module with code like this:
define(function (require) {
var http = require('plugins/http'),
ko = require('knockout');
//other stuff
}
当我在浏览器中运行代码时,一切都很好,但是我想看看是否可以让TypeScript知道类型。从DefinitelyTyped导入了Durandal,Knockout,jQuery和RequireJS NuGet程序包,通过执行此操作,我可以得到输入的ko变量
Everything is working fine when I run the code in the browser, but I wanted to see if I could get TypeScript to be aware of the types. I imported the Durandal, Knockout, jQuery, and RequireJS NuGet packages from DefinitelyTyped and I was able to get the ko variable typed by doing
ko : KnockoutStatic = require('knockout');
之所以可行,是因为KnockoutStatic是在Knockout.d.ts文件,但是,在durandal.d.ts文件中,这样声明 plugins / http,没有命名接口:
This works because KnockoutStatic is an interface declared in the Knockout.d.ts file. However, in the durandal.d.ts file, 'plugins/http' is declared like this with no named interface:
declare module 'plugins/http' { /*stuff*/ }
I有三个问题:
- Sh是否有办法让TypeScript根据带有模块名称的 require调用自动知道ko和http的类型?
- 有没有办法显式键入我的http变量,其格式与durandal.d.ts文件的结构方式有关(模块与接口)。
- 如果没有,那么设置durandal.d.ts文件的正确方法是什么?允许强类型输入http变量-只需声明一个接口而不是一个模块?
推荐答案
所有三个问题的答案:
import http = require('plugins/http');
另外,您不应该使用手动定义,因为打字稿会为您生成。这些称为外部模块。
Additionally you should not have a manual "define" since typescript will generate it for you. These are called external modules.
PS:我有一段有关打字稿模块系统的视频 http://youtube.com/watch?hd=1&v=KDrWLMUY0R0
PS : I have a video about typescripts module system http://youtube.com/watch?hd=1&v=KDrWLMUY0R0
这篇关于使用TypeScript中已声明模块的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!