【问题标题】:In typescript, how can I inherit from an external JS library using a declaration file在打字稿中,如何使用声明文件从外部 JS 库继承
【发布时间】:2016-05-19 13:01:15
【问题描述】:

我正在尝试为外部库 (https://github.com/datastax/nodejs-driver/) 编写 .d.ts 文件。

我可以映射函数/对象,但我看不到如何映射“从 JS 库中定义的对象继承”逻辑(例如:http://docs.datastax.com/en/developer/nodejs-driver/2.1/nodejs-driver/reference/addressResolution.html

我没有找到任何与此相关的内容,知道如何实现吗?

【问题讨论】:

    标签: node.js typescript


    【解决方案1】:

    在库以及定义中,您尝试继承的库需要是类或构造函数(在 js 中)或 TypeScript 中的类、构造函数或接口。

    这实际上是在 React 中使用 TypeScript 时非常常见的场景:

    class TodoApp extends React.Component<IAppProps, IAppState> {
    

    查看 node-casandra 驱动程序的 API,您应该可以做到这一点。

    这是一个幼稚的例子:

    // cassandra-driver.d.ts
    declare module "cassandra-driver" { 
        export class Client {}
    }
    

    然后:

    import {Client} from 'cassandra-driver';
    
    class Foo extends Client {
    
    }
    

    我建议reading up 创建 d.ts 定义。将您的定义创建为外部模块——而不是环境定义。

    那么您应该与TypingsDefinatlyTyped 或这两个项目共享您的打字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-16
      • 2014-07-30
      • 2016-10-09
      • 1970-01-01
      • 2021-04-26
      • 2017-12-04
      • 2016-11-07
      • 1970-01-01
      相关资源
      最近更新 更多