Skip to content

Typescript incompatible type in 3.1.0 and Express 4.x #340

Closed

Description

@aabrams

Trying to add in Express 4.x middleware:

import { Receiver, CloudEvent } from "cloudevents";

const receivedEvent: CloudEvent = Receiver.accept(req.headers, req.body);

It fails with:

(property) IncomingMessage.headers: IncomingHttpHeaders
Argument of type 'IncomingHttpHeaders' is not assignable to parameter of type 'Headers'.
Index signatures are incompatible.
Type 'string | string[]' is not assignable to type 'string'.
Type 'string[]' is not assignable to type 'string'.ts(2345)

Activity

  1. lance commented on Sep 8, 2020

    @lance
    Member

    It appears that the IncomingHttpHeaders type is from Node core, so I think this will probably be a common issue. We should consider modifying our type.

    export interface Headers {
    [key: string]: string;
    }

    In the meantime @aabrams can you try

    const receivedEvent: CloudEvent = Receiver.accept(req.headers as Headers, req.body);
  2. aabrams commented on Sep 9, 2020

    @aabrams
    Author

    Trying what you suggested (req.headers as Headers):

    Argument of type 'Headers' is not assignable to parameter of type 'import("c:/Users/adam_a/repos/Segment-Webhook/node_modules/cloudevents/dist/transport/http/headers").Headers'.
    Index signature is missing in type 'Headers'.ts(2345)
    Conversion of type 'IncomingHttpHeaders' to type 'Headers' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)
    Conversion of type 'IncomingHttpHeaders' to type 'Headers' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
    Type 'IncomingHttpHeaders' is missing the following properties from type 'Headers': append, delete, get, has, and 6 more.ts(2352)

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