Skip to content

The CloudEvent type should be generic #445

Closed

Description

@matthewrobertson

As a developer I usually know what the data payload of my cloudevent will be when I get my hands on it. It would be great if I could express this in typescript to enable better type checking and auto completion:

interface MyPayload {
  foo: string;
  bar: number;
}

const myHandler = (e: CloudEvent<MyPayload>) => {
  e.data.foo; // OK
  e.data.banana; // Type Error!
};

IMO the existing type hint for the data field is not adding much value:

data?: Record<string, unknown | string | number | boolean> | string | number | boolean | null | unknown;

This would be better expressed as something like:

export interface CloudEventV1OptionalAttributes<T> {
  // ...
  data?: T;
}

Activity

  1. lance commented on Nov 20, 2021

    @lance
    Member

    @matthewrobertson thank you for the suggestion! I agree this is quite nice. While I feel pretty strong about my Node.js chops, please forgive my relative inexperience with TypeScript. (This project was converted to TS at the request of the community)

    I just dropped a PR that should do what you want. IIUC this would not be a breaking change from our mishmash type union. Can you please confirm that, and whether the PR accomplishes your aims.

    #446

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