Use typings from a declared module in TypeScript

我与影子孤独终老i 提交于 2019-12-25 05:14:11

问题


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
}

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');

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 have three questions:

  • Should/is there a way to get TypeScript to know the types for ko and http automatically based on the call to require with the module name inside?
  • Is there a way to explicitly type my http variable with the way that the durandal.d.ts file is structured (module vs interface).
  • If not, what would be the right way to set up the durandal.d.ts file to allow strong typing of the http variable - just declare an interface instead of a module?

回答1:


The answer for all three questions :

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 : I have a video about typescripts module system http://youtube.com/watch?hd=1&v=KDrWLMUY0R0



来源:https://stackoverflow.com/questions/22128598/use-typings-from-a-declared-module-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!