如何扩展在外部库 d.ts 中声明的接口? [英] How to extend an interface declared in an external library d.ts?

查看:29
本文介绍了如何扩展在外部库 d.ts 中声明的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这样的文档化方法安装了 knockout 定义.

npm install @types/knockout

效果很好,我可以像这样在任何地方导入它.

import * as ko from "knockout";

然而,我坚持使用一些自定义内容扩展 KnockoutStatic 接口.我正在尝试迁移基于 namespace 的大型 TS 应用程序以使用模块.之前,我很容易地声明了扩展接口anywhere,并且这些声明被合并了.假设我的扩展程序看起来像这样.

interface KnockoutStatic {doSomething():无效;}

我尝试创建一个 KnockoutExtensions.d.ts 文件,并在其中声明它.

import "knockout";声明模块淘汰赛"{导出接口 KnockoutStatic {doSomething():无效;}}

但是当我在某处导入 knockout 和我的扩展时,TS 仍然无法解析 doSomething 调用.

import * as ko from "knockout";导入./KnockoutExtensions";ko.doSomething();//错误

使用 TypeScript 2.0 和新的 d.ts 子系统扩展库接口的正确方法是什么?

我使用的是安装了 TypeScript 2.0 的 Visual Studio 2015 Update 3.

解决方案

您可以轻松扩展 'knockout' 或任何其他 TypeScript 命名空间.

示例:创建knockout-extension.d.ts 文件

///声明模块淘汰赛"{导出接口 CustomType {自定义字段:字符串;customMethod(arg1: number, arg2: boolean): boolean;}命名空间 customNamespace {导出接口 AnotherCustomType {customField1:字符串;customField2:布尔值;}}//注意:扩展现有接口导出接口 KnockoutStatic {自定义方法():无效;}}

<块引用>

注意:确保这个文件被 TypeScript 编译器选中.

使用扩展模块中新定义的类型.

//一种方式从淘汰赛"导入 { CustomType };const foo:自定义类型;//第二种方式import * as kc from 'knockout';const foo: kc.CustomType;常量栏:kc.customNamespace.AnotherCustomType;

有关模块和命名空间的更多信息,您可以查看 Modules 上的 TypeScript 文档和 命名空间 并使用它们 一起.

干杯!

I installed the knockout definitions using the documented method like this.

npm install @types/knockout

It works nicely, I could import it like this anywhere.

import * as ko from "knockout";

However, I'm stuck with extending KnockoutStatic interface with some custom stuff. I'm trying to migrate a <reference ... /> and namespace based huge TS application to use modules. Before, I easily declared the extension interface anywhere and the declarations got merged. Let's say my extension looks like this.

interface KnockoutStatic {
  doSomething(): void;
}

I tried to create a KnockoutExtensions.d.ts file where I declared it like this.

import "knockout";

declare module "knockout" {
  export interface KnockoutStatic {
    doSomething(): void;
  }
}

But when I import both knockout and my extension somewhere, TS still cannot resolve the doSomething calls.

import * as ko from "knockout";
import "./KnockoutExtensions";

ko.doSomething(); // error

What is the proper method of extending library interfaces using TypeScript 2.0 and the new d.ts subsystem?

I'm using Visual Studio 2015 Update 3 with TypeScript 2.0 installed.

解决方案

You can easily extend the 'knockout' or any other TypeScript namespace.

Example: create knockout-extension.d.ts file

/// <reference path="<path-to-typings-dir>/knockout/index.d.ts" />

declare module 'knockout' {

  export interface CustomType {

    customField: string;

    customMethod(arg1: number, arg2: boolean): boolean;
  }

  namespace customNamespace {

    export interface AnotherCustomType {
      customField1: string;
      customField2: boolean;
    }
  }

  // NOTE: extending existing interface
  export interface KnockoutStatic {
    customMethod(): void;
  }
}

Note: ensure that this file is picked-up by the TypeScript compiler.

Use the newly defined types from the extended module.

// one way
import { CustomType } from 'knockout';

const foo: CustomType;

// second way
import * as kc from 'knockout';

const foo: kc.CustomType;
const bar: kc.customNamespace.AnotherCustomType;

For more info on modules and namespaces you can check TypeScript documentation on Modules and Namespaces and using them together.

Cheers!

这篇关于如何扩展在外部库 d.ts 中声明的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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