Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

TypeScript usage? #18

Closed
milibopp opened this issue Apr 6, 2022 · 2 comments
Closed

TypeScript usage? #18

milibopp opened this issue Apr 6, 2022 · 2 comments

Comments

@milibopp
Copy link

milibopp commented Apr 6, 2022

I tried using channel with TypeScript and found it to fail type checking.

With the following code (mostly from the README):

import Channel = require("@nodeguy/channel");
import assert = require("assert");

const channel = Channel();

const send = async () => {
  await channel.push(42);
  await channel.close();
};

const receive = async () => {
  assert.equal(await channel.shift(), 42);
  assert.equal(await channel.shift(), undefined);
};

(async () => {
  await Promise.all([send(), receive()]);
})();

I ran the script with ts-node and got this type error:

TSError: ⨯ Unable to compile TypeScript:
foo.ts:5:17 - error TS2349: This expression is not callable.
  Type 'typeof import("redacted/path/node_modules/@nodeguy/channel/lib/index")' has no call signatures.

5 const channel = Channel();
                  ~~~~~~~

It runs fine, when I add // @ts-ignore before the offending line, so it seems the module itself is fine? Still I'd like not to use that hack and have it typecheck properly ;)

Am I just using this wrong? Perhaps @graup could weigh in, as they added the type definitions.

@graup
Copy link
Contributor

graup commented Apr 7, 2022

Are you sure this isn't an issue with your setup? Do you get the same error when using ESM-style imports?

Copy-pasting your code (changing the imports to ESM style) to Typescript Playground, I don't get any errors there.

See Typescript Playground

@milibopp
Copy link
Author

milibopp commented Apr 7, 2022

Ah, that's it. Thanks for questioning it. I needed to use the esModuleInterop compiler option for TypeScript to accept that import, but now it works.

@milibopp milibopp closed this as completed Apr 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants