Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
aabrams opened this issue Sep 8, 2020 · 2 comments · Fixed by #346
Closed

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

aabrams opened this issue Sep 8, 2020 · 2 comments · Fixed by #346

Comments

@aabrams
Copy link

aabrams commented Sep 8, 2020

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)

@lance
Copy link
Member

lance commented Sep 8, 2020

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);

@aabrams
Copy link
Author

aabrams commented Sep 9, 2020

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)

lance added a commit to lance/sdk-javascript that referenced this issue Oct 1, 2020
Fixes: cloudevents#340

Signed-off-by: Lance Ball <lball@redhat.com>
@lance lance closed this as completed in #346 Oct 6, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants