如何使用打字稿编写节点模块? [英] How do you write a node module using typescript?

查看:64
本文介绍了如何使用打字稿编写节点模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,另一个问题(如何使用打字稿导入模块)的一般答案是:



1)创建一个blah.d.ts定义文件。



2)使用:

  ///<参考路径= ./ defs / foo / foo.d.ts /> 
import foo = require( foo);

重要的是,您需要两者文件foo.d.ts和a foo.js在您的node_modules中加载; NAME foo必须完全匹配。现在...



我想回答的问题是如何编写可以以这种方式导入的打字稿模块? / p>

让我说我有这样一个模块:

 -xq / 
-xq / defs / Qdts
-xq / index.ts
-xq / base.ts
-xq / thing.ts

我想从base.ts导出模块'xq',其类为'Base',从Thing.ts导出'Thing'。 / p>

如果这是javascript中的节点模块,则我的index.ts类似于:

  var base = require('./ base'); 
var something = require(’./ thing’);
module.exports = {
Base:base.Base,
Thing:thing.Thing
};

让我们尝试使用类似的打字稿文件:

  import base = require('./ base'); 
导出模块xq {
export var base = base.Base;
}

调用它:

  tsc base.ts index.ts something.ts ... --sourcemap --declaration --target ES3 
--module commonjs --outDir dist / xq

会发生什么?好吧,我们得到了base.d.ts:

  export声明class Base< T> {
...
}

和无用的索引。d。 ts:

  export声明模块xq {
var基数:任意; //没有类型提示!大。 :(
}

并且完全无效的javascript不会导入模块:

 (函数(xq){
xq.base = xq.base.Base;
})( exports.xq ||(exports.xq = {}));
var xq = exports.xq;

我已经尝试过很多主题的变体,唯一可以使用的是:

 声明var require; 
var base = require('./ base');
导出模块xq {
export var base = base.Base;
}

...但这显然完全破坏了类型检查器。



所以。



打字稿很棒,但是这个模块的东西完全烂透了。



1)是否可能与内置的定义生成器(我很可疑)



2)如何手动操作?我已经在.d.ts文件中看到了import语句,我认为这意味着有人已经找到了解决方法。这些工作如何?您如何为其中带有声明的模块编写打字稿?



(例如,我怀疑执行模块声明的正确方法是:

  ///< reference path = base.d.ts /> 
声明模块 xq {{
import base = require('./ base');
module xq {
//一些如何导出符号base.Base为Base
}
export = xq;
}

...但我不知道打字稿是什么

解决方案

对于JavaScript:

  var base = require('./ base'); 
var something = require(’./ thing’);
module.exports = {
Base:base.Base,
Thing:thing.Thing
};

TypeScript:

  import base = require('./ base'); 
进口东西= require(’./ thing’);
var toExport = {
Base:base.Base,
Thing:thing.Thing
};
export = toExport;

甚至是这个打字稿:

  import base = require('./ base'); 
进口东西= require(’./ thing’);
export var Base = base.Base;
export var Thing = something.Thin;


So, the general answer for the other question (How do you import a module using typescript) is:

1) Create a blah.d.ts definition file.

2) Use:

/// <reference path="./defs/foo/foo.d.ts"/>
import foo = require("foo");

Critically, you need both the files foo.d.ts and a foo.js somewhere in your node_modules to load; and the NAME foo must exactly match for both. Now...

The question I would like to have answered is how to you write a typescript module that you can import that way?

Lets say I have a module like this:

- xq/
- xq/defs/Q.d.ts
- xq/index.ts
- xq/base.ts
- xq/thing.ts

I want to export the module 'xq' with the classes 'Base' from base.ts, and 'Thing' from thing.ts.

If this was a node module in javascript, my index.ts would look something like:

var base = require('./base');
var thing = require('./thing');
module.exports = {
  Base: base.Base,
  Thing: thing.Thing
};

Let's try using a similar typescript file:

import base = require('./base');
export module xq {
    export var base = base.Base;
}

Invoke it:

tsc base.ts index.ts things.ts ... --sourcemap --declaration --target ES3 
                                   --module commonjs --outDir dist/xq

What happens? Well, we get our base.d.ts:

export declare class Base<T> {
  ...
}

and the thrillingly unuseful index.d.ts:

export declare module xq {
    var Base: any; // No type hinting! Great. :(
}

and completely invalid javascript that doesn't event import the module:

(function (xq) {
    xq.base = xq.base.Base;
})(exports.xq || (exports.xq = {}));
var xq = exports.xq;

I've tried a pile of variations on the theme and the only thing I can get to work is:

declare var require;
var base = require('./base');
export module xq {
    export var base = base.Base;
}

...but that obviously completely destroys the type checker.

So.

Typescript is great, but this module stuff completely sucks.

1) Is it possible to do with the built in definition generator (I'm dubious)

2) How do you do it by hand? I've seen import statements in .d.ts files, which I presume means someone has figured out how to do this; how do those work? How do you do the typescript for a module that has a declaration with an import statement in it?

(eg. I suspect the correct way to do a module declaration is:

/// <reference path="base.d.ts" />
declare module "xq" {
  import base = require('./base'); 
  module xq {
    // Some how export the symbol base.Base as Base here
  }
  export = xq;
}

...but I have no idea what the typescript to go along that would be).

解决方案

For JavaScript :

var base = require('./base');
var thing = require('./thing');
module.exports = {
  Base: base.Base,
  Thing: thing.Thing
};

TypeScript :

import base = require('./base');
import thing = require('./thing');
var toExport = {
  Base: base.Base,
  Thing: thing.Thing
};
export = toExport;

Or even this typescript:

import base = require('./base');
import thing = require('./thing');
export var Base = base.Base;
export var Thing = thing.Thin;

这篇关于如何使用打字稿编写节点模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆