简体   繁体   English

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

[英]Typescript cannot find name from imported constructor

I have a constructor that is imported from another module, but Typescript complains that it cannot find the name, despite it being clearly in scope. 我有一个从另一个模块导入的构造函数,但是Typescript抱怨尽管名称范围很广,但找不到该名称。 A small example: 一个小例子:

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
);

When I try to compile (or run with ts-node ), I get this error: 当我尝试编译(或与ts-node一起运行)时,出现此错误:

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

If I comment the last console.log() line out, the console does properly log out that NoteOnEvent as the class constructor. 如果我注释掉最后一个console.log()行,则控制台会正确注销该NoteOnEvent作为类构造函数。 It seems like only the type assertion cannot find the name. 似乎只有类型断言无法找到名称。 Anyone know why? 有人知道为什么吗?

You have a value named NoteOnEvent here, but no type with this name in scope. 您在此处有一个名为NoteOnEvent ,但没有作用域内具有该名称的类型 The const line won't destructure out a type; const行不会破坏类型。 you can write 你可以写

type NoteOnEvent = MIDIEvents.NoteOnEvent;

assuming that type actually exists 假设该类型确实存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM