更改从类型定义导入的类型 [英] Change type imported from typings definition

查看:24
本文介绍了更改从类型定义导入的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Typescript,并试图让编译器让我在以下内容中执行 replServer.context.something = 123:

I'm just getting started with Typescript and am trying to get the compiler to let me do replServer.context.something = 123 in the following:

import repl = require('repl');

let replServer = repl.start({
  prompt: '>> '
});

replServer.context.something = 123;

但它抱怨:类型'EventEmitter'上不存在属性'上下文'".

but it's complaining with: "Property 'context' does not exist on type 'EventEmitter'".

我已经用 typings install dt~node --save --global 安装了类型定义,repl 模块在 typings/globals 中定义如下/node/index.d.ts:

I have installed the type definitions with typings install dt~node --save --global and the repl module is defined as follows in typings/globals/node/index.d.ts:

declare module "repl" {
    import * as stream from "stream";
    import * as events from "events";

// ...
    export function start(options: ReplOptions): events.EventEmitter;
}

我认为该解决方案涉及定义我自己的类型扩展 events.EventEmitter,它具有类型为 any(对象?)的 context,然后以某种方式覆盖typings/globals/node/index.d.ts 使用新类型.

I would imagine the solution involves defining my own type extending events.EventEmitter which has a context of type any (object?), and then somehow overriding the definition in typings/globals/node/index.d.ts to use the new type.

这样对吗?它是如何完成的?(我想你会离开 typings/globals/node/index.d.ts 并在某处添加另一个文件(可能在 typings 目录之外)).

Is this right? How is it done? (I would imagine you leave typings/globals/node/index.d.ts alone and add another file somewhere (probably outside typings directory)).

Typescript 读取这些定义文件(index.d.ts 和覆盖此定义的文件)的顺序重要吗?

Does the order in which Typescript read these definition files (index.d.ts and the one which overrides this definitions) matter?

谢谢!

编辑 1:

我最终尝试了我的想法并且它正在工作(但这取决于目录命名......即黑客).

I ended up trying what I had in mind and it's working (but it depends on directory naming... i.e. a hack).

我添加了 typings2/node/index.d.ts 如下:

declare module "repl" {
    import * as events from "events";

    interface ReplEventEmitter extends events.EventEmitter {
      context: any;
    }

    export function start(options: ReplOptions): ReplEventEmitter;
  }

它工作正常(即保留以前的定义并添加context),但这取决于我将其放入的目录被命名为之后 <代码>打字.

It works fine (i.e. previous definitions are kept and a context is added), but it depends on the directory I put it in being named something which comes after typings.

我正在使用 Atom,我使用的插件会自动更改 tsconfig.json 中的文件"字段,以列出要编译的文件.

I'm using Atom and the plugin I'm using automatically changes the "files" field in tsconfig.json to list the files to be included for compilation.

如果我的文件出现在文件"(由 Atom 插件管理)中的 typings/globals/node/index.d.ts 之前,这将不起作用.它需要跟在它之后(否则,这会生效:export function start(options: ReplOptions): events.EventEmitter;).

If my file comes before typings/globals/node/index.d.ts in "files" (managed by Atom plugin), this doesn't work. It needs to come after it (otherwise, this takes effect: export function start(options: ReplOptions): events.EventEmitter;).

这样做的正确"方法是什么?

What's the "proper" way of doing this?

推荐答案

这样做的正确"方法是什么?

What's the "proper" way of doing this?

外部模块定义受到锁定的影响.如果支持context,建议您对原始定义文件进行PR.

External module definitions suffer from getting locked. If context is something is supported, suggest you make a PR to the original definition file.

创建typings 文件的本地副本,不再依赖于上游版本.我也这样做:https://github.com/alm-tools/alm/tree/master/src/typings

Create a local copy of the typings file and no long depend on the upstream version. I do this as well : https://github.com/alm-tools/alm/tree/master/src/typings

仍然比 JavaScript 好(根本没有帮助).

Still better than JavaScript (no help at all).

这篇关于更改从类型定义导入的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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