打字稿无法从导入的构造函数中找到名称

时间:2019-02-19 23:25:06

标签: typescript typescript-typings

我有一个从另一个模块导入的构造函数,但是Typescript抱怨尽管名称范围很广,但找不到该名称。一个小例子:

import {
  MIDIEvents, IMIDIChannelEvent, MIDIEventType,
} from "@thayes/midi-tools";

const {
  NoteOnEvent,
} = MIDIEvents;

console.log({
  NoteOnEvent,
});

const ev:IMIDIChannelEvent = {
  channel: 1,
  type: MIDIEventType.NoteOn,
};

console.log(
  (ev as NoteOnEvent).channel
);

当我尝试编译(或使用ts-node运行)时,出现此错误:

TSError: ⨯ Unable to compile TypeScript:
test.ts(20,5): error TS2304: Cannot find name 'NoteOnEvent'.

如果我注释了最后的console.log()行,则控制台会正确注销该NoteOnEvent作为类构造函数。似乎只有类型断言无法找到名称。有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

您在此处有一个名为NoteOnEvent,但是在范围内没有该名称的 type const行不会破坏类型。你可以写

type NoteOnEvent = MIDIEvents.NoteOnEvent;

假设该类型确实存在

相关问题