Ale*_*ler 1 typescript typescript-typings
我有一个具有这个 ES6 类签名的第三方库:
class Machine {
constructor(options)
static list(callback)
create(options, callback)
}
Run Code Online (Sandbox Code Playgroud)
我试图为这个类创建类型声明,但出现了一些错误:
export declare class IMachine {
public constructor(opts: MachineOptions)
public static list(callback: (err?: Error, machines?: IMachine[]) => void): void
}
declare interface MachineOptions {
name: string
}
Run Code Online (Sandbox Code Playgroud)
用法:
const Machine: IMachine = require('lib')
Machine.list((err: Error, machines: IMachine[]) => { } // error TS2576: Property 'list' is a static member of type 'IMachine'
const machine = new Machine({name: 'some name'}) // error TS2351: This expression is not constructable. Type 'IMachine' has no construct signatures.
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
你的声明没问题。问题是这一行:
const Machine: IMachine = require('lib')
Run Code Online (Sandbox Code Playgroud)
IMachine实际上是指类的实例的类型,而不是类(构造函数)本身。
相反,你会想要使用typeof IMachine:
const Machine: typeof IMachine = require('lib')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |