1

I'm implementing subscriptions in Apollo Server using TypeScript and a PubSub class for event handling. However, I've encountered an issue when trying to use a named interface for the PubSub generic type.

When using a named interface, I encounter the following error:

interface PubSubEvents {
  CHANGE_EVENT: string;
}

export interface Context {
  pubsub: PubSub<PubSubEvents>; 
  // Error: Type 'PubSubEvents' does not satisfy the constraint '{ [event: string]: unknown; }'.
}

Interestingly, the same code works fine when using an inline type definition:

export interface Context {
  pubsub: PubSub<{
    CHANGE_EVENT: string;
  }>;
}

Why does the inline type definition work, but the named PubSubEvents interface fails? Is this a limitation of TypeScript's type system or an issue with how PubSub is implemented? How can I fix this while keeping the named interface?

1 Answer 1

1

You can use:

type PubSubEvents = {
  CHANGE_EVENT: string;
}

or add an explicit index signature:

interface PubSubEvents {
  CHANGE_EVENT: string;

  [event: string]: unknown;
}

References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.