简体   繁体   中英

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. 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:

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. 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. The const line won't destructure out a type; you can write

type NoteOnEvent = MIDIEvents.NoteOnEvent;

assuming that type actually exists

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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