【发布时间】:2021-02-09 19:42:11
【问题描述】:
我有一个第三方 ES6 类的 require 语句,如下所示:
const Machine: typeof IMachine = require('lib')
IMachine 是我对导入类的类型定义:
export declare class IMachine {
public constructor(opts: MachineOptions)
public static list(callback: (err?: Error, machines?: IMachine[]) => void): void
}
declare interface MachineOptions {
name: string
}
如何使用import 语句来表达这一点?
更新
基于Aluans answer我创建了一个repro:https://github.com/AlexZeitler/sourced-typings-repro
我得到的编译器错误:
src/Market.ts:11:10 - error TS2339: Property 'rehydrate' does not exist on type 'Market'.
11 this.rehydrate(snapshot, events)
~~~~~~~~~
src/Market.ts:11:20 - error TS2552: Cannot find name 'snapshot'. Did you mean 'snapshots'?
11 this.rehydrate(snapshot, events)
~~~~~~~~
src/Market.ts:6:15
6 constructor(snapshots?, events?) {
~~~~~~~~~~
'snapshots' is declared here.
src/Market.ts:15:10 - error TS2339: Property 'id' does not exist on type 'Market'.
15 this.id = param.id
~~
src/Market.ts:16:10 - error TS2339: Property 'digest' does not exist on type 'Market'.
16 this.digest('init', param)
~~~~~~
src/Market.ts:17:10 - error TS2339: Property 'emit' does not exist on type 'Market'.
17 this.emit('initialized', param, this)
import 语句显示此警告:
Could not find a declaration file for module 'sourced'. '/Users/alex/src/sourced-typings-repro/node_modules/sourced/dist/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/sourced` if it exists or add a new declaration (.d.ts) file containing `declare module 'sourced';`ts(7016)
原始问题中的示例基于我的另一个问题,该问题导致关于 typeof + require 与导入样式的问题:
【问题讨论】:
-
import { Imachine } from 'lib'? -
你不能:ES import 没有类型的概念,TS 没有扩展它的语法。
标签: typescript