typescript 为默认导出模块编写声明文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39109027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:46:58  来源:igfitidea点击:

Write a declaration file for a default export module

typescripttypescript-typings

提问by julien_c

I have a npm module called RiveScript that usually (in Javascript) gets instantiated that way:

我有一个名为 RiveScript 的 npm 模块,它通常(在 Javascript 中)以这种方式实例化:

var RiveScript = require('rivescript');
var rivescript = new RiveScript();

I'm trying to write a declaration file for the module, but am stuck at the first step. Here's what I've written so far:

我正在尝试为模块编写声明文件,但在第一步中卡住了。这是我到目前为止所写的内容:

declare module "rivescript" {

    interface RivescriptOptions {
        utf8?: boolean;
    }

    class RiveScript {
        constructor(options?: RivescriptOptions);
    }

    export default RiveScript;
}

Then I guess in Typescript I would be using the module this way (default import):

然后我想在 Typescript 中我会以这种方式使用模块(默认导入):

import RiveScript from 'rivescript';
let rivescript = new RiveScript();

However, tscgenerates this, which is not valid as it references a default()function:

但是,tsc生成这个,这是无效的,因为它引用了一个default()函数:

const rivescript_1 = require('rivescript');
let rivescript = new rivescript_1.default();

What am I doing wrong?

我究竟做错了什么?

回答by Pelle Jacobs

You're really close. Instead of using export default, you should use export =.

你真的很亲近。而不是使用export default,你应该使用export =.

custom-typings/rivescript.d.ts

自定义类型/rivescript.d.ts

declare module 'rivescript' {
  class RiveScript {
    constructor()
  }
  export = RiveScript
}

app.js

应用程序.js

import RiveScript = require('rivescript');
let rivescript = new RiveScript();

For more info on how to write declaration files, you should have a look at the Typescript Handbook. Eg. they have a template for 'exporting modules as a class.

有关如何编写声明文件的更多信息,您应该查看Typescript 手册。例如。他们有一个“将模块作为一个类导出”的模板。

回答by toffee

Accepted anwser works well for this question. But I want to give another idea

接受的 anwser 很适合这个问题。但我想给出另一个想法

if you want to extendsclass, add dynamic method to class at runtime.

如果要扩展类,请在运行时向类添加动态方法。

you can try (src/plugins/processor.ts)

你可以试试 (src/plugins/processor.ts)

    import server from '../../server'

    declare module "../../server"{
        export default interface server{
            process() // it's a new method of server
        }
    }

    export default class{ //another codes, just for show I create a 'server' instance
        private instance:server
        constructor{
            this.instance = new server()
            this.instance.process() //works
        }
    }

in my server.ts(src/server.ts),

在我的server.ts(src/server.ts)中,

my default export class signature

我的默认导出类签名

    export default class Server extends extend implements Startable<void>

Because Server is a default export, so you can change serverin export default interface serverto any valid variable name

由于服务器是一个default export,所以你可以改变serverexport default interface server任何有效的变量名

for example:

例如:

export default interface anotherName