Pregunta

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?
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top