Skip to content

Incorrect typings of @serialport/bindings-cpp when using ESM #2705

Open

Description

@CalmDownVal

SerialPort Version

12.0.1

Node Version

20.6.1

Electron Version

No response

Platform

macOS Ventura 13.4.1

Architecture

ARM

Hardware or chipset of serialport

No response

What steps will reproduce the bug?

  • Install @serialport/bindings-cpp via Yarn Berry 3.6.3 with PnP
  • Use with TypeScript 5.2.2, set up with --module ESNext and --module-resolution bundler
  • Import the AutoDetectTypes type

What happens?

The type is reduced to any as TypeScript fails to load the BindingInterface type from @serialport/bindings-interface

What should have happened?

The type union should get correctly imported and not get reduced to any.

Additional information

I'm aware that Yarn PnP is an experimental feature, however that doesn't seem to be the cause, other modules work totally fine. It seems to be simply an out of date/incomplete package.json exports definition of the @serialport/bindings-interface package.

There is a pull request for the very package, soon about to celebrate its first birthday, that resolves this issue: serialport/bindings-interface#32

I'm opening this issue as the author of the mentioned PR seems to not have done so, or perhaps missed the readme asking to open issues here instead.

Activity

  1. changed the title [-]Can't import @serialport/bindings-cpp when using ESM[/-] [+]Incorrect typings of @serialport/bindings-cpp when using ESM[/+] on Sep 16, 2023
  2. yinghaochan commented on Feb 27, 2026

    @yinghaochan

    without patching, we can re-export the SerialPort class

    // copy and paste the contents of node_modules/@serialport/bindings-interface/dist/index.d.ts here
    
    import { AutoDetectTypes } from "@serialport/bindings-cpp";
    import { StreamOptions } from "@serialport/stream";
    import { SerialPort } from "serialport";
    
    export type SerialPortOpenOptions<T extends AutoDetectTypes> = Omit<
        StreamOptions<T>,
        "binding"
    > &
        OpenOptionsFromBinding<T>;
    class SerialPortWrapper extends SerialPort {
        constructor(options: SerialPortOpenOptions<AutoDetectTypes>) {
            super(options);
        }
        static list(): Promise<PortInfo[]> {
            return super.list();
        }
    }
    
    export { SerialPortWrapper as SerialPort };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions