1 year ago

#269876

test-img

gamliela

Typescript: can't augment namespace

I'm using "amazon-connect-streams" npm package. The package has a few mismatches between the type definition and the javascript runtime.

I'm trying to override the bad typing in my project, using namespace augmentation, but can't figure out how to do it.

This is the code that fails:

import "amazon-connect-streams";

function test() {
  connect.agent(() => {}).unsubscribe(); // <-- TS2339: Property 'unsubscribe' does not exist on type 'void'.
}

It fails because agent is defined in "amazon-connect-streams" library this way:

declare namespace connect {
  type AgentCallback = (agent: Agent) => void;

  function agent(callback: AgentCallback): void;

  \\ ...
}

However, the function actually do return a value. So I tried to override the function definition, but this now fails with a different error:

import "amazon-connect-streams";

declare namespace connect {
  interface ConnectUnsubscribe {
    unsubscribe: () => void;
  }

  function agent(callback: connect.AgentCallback): ConnectUnsubscribe; // <-- TS2694: Namespace 'connect' has no exported member 'AgentCallback'.
}

function test() {
  connect.agent(() => {}).unsubscribe();
}

Is there a way to solve this error, without redefining the whole namespace?

typescript

.d.ts

module-augmentation

0 Answers

Your Answer

Accepted video resources