简体   繁体   English

如何加载定义为内部模块(TSD)的外部TypeScript模块

[英]How to load an external typescript module that is defined as an internal module (tsd)

I am trying to load the nouislider module in my project: Angular 2 beta-0, Typescript 1.7 using SystemJs 0.19.4. 我试图在我的项目中加载nouislider模块:Angular 2 beta-0,使用SystemJs 0.19.4的Typescript 1.7。

I have it working with a work-around: by changing the .d.ts file tsd has installed for me. 我有一种解决方法:通过更改.d.ts文件, tsd已为我安装了。 I would like to know what, if anything, I can do to load the module without altering the original typescript definition file: nouislider.d.ts . 我想知道在不更改原始打字稿定义文件: nouislider.d.ts的情况下,如何加载模块。

With my changes I can currently load nouislider and use it in my Typescript project with SystemJs like so: 通过nouislider更改,我现在可以加载nouislider并将其用于SystemJ的Typescript项目中,如下所示:

import {create as createSlider}  from 'noUiSlider/distribute/nouislider';
createSlider(document.getElementById('test-slider'), {
  start: 40,  connect: "lower",
  range: { 'min': 0, 'max': 100 }
});

What I did to get it working was change these two lines at the top in nouislider.d.ts : 我要做的就是在nouislider.d.ts的顶部更改这两行:

declare module noUiSlider {
    function create(target: HTMLElement, options: Options): void;

into

declare module 'noUiSlider/distribute/nouislider' {
    export function create(target : HTMLElement, options : Options) : void;

I set the module's name to 'noUiSlider/distribute/nouislider', otherwise System.js does not load the module from the right path. 我将模块的名称设置为“ noUiSlider / distribute / nouislider”,否则System.js不会从正确的路径加载模块。 And I export the create function, since otherwise I have no way of loading it 我导出了create函数,因为否则我无法加载它

How can I prevent making these two changes? 如何防止进行这两项更改? I would be quite content creating an external module definition myself that references the internal module definition or achieve the same result some other way without changing nouislider.d.ts. 我自己创建一个引用内部模块定义的外部模块定义或以其他方式实现相同结果而无需更改nouislider.d.ts会很满足。 I've tried a bunch of things, but couldn't make it work, and seem to be getting lost in all the docs. 我已经尝试了很多方法,但是无法使其正常工作,并且似乎在所有文档中迷失了方向。 Your help is much appreciated. 非常感谢您的帮助。

No much you can do I am afraid other than creating a pull request on DefinitelyTyped and propose adding 除了在DefinitelyTyped上创建拉取请求并建议添加外 ,您无所畏惧

declare module 'noUiSlider/distribute/nouislider' {
     export = noUiSlider;
}

This should simply add the possibility to use it as an external module without breaking any existing use 这应该只是增加了将其用作外部模块的可能性,而不会破坏任何现有用途

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM