Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

IHeaders type definition #924

Closed
Pipo93 opened this issue Oct 15, 2020 · 0 comments · Fixed by #927
Closed

IHeaders type definition #924

Pipo93 opened this issue Oct 15, 2020 · 0 comments · Fixed by #927

Comments

@Pipo93
Copy link

Pipo93 commented Oct 15, 2020

Issue

Currently IHeaders are defined to contain strings or Buffers:

kafkajs/types/index.d.ts

Lines 98 to 100 in 4139127

export interface IHeaders {
[key: string]: Buffer | string
}

When consuming a message within a typescript project this may cause some issues when accessing header keys which are not available, e.g.: const testHeader = headers?.test.toString().
In this case typescript wouldn't complain but the application will crash when test header is not set.

Suggested solution:

IHeader keys should also be typed with undefined:

export interface IHeaders {
  [key: string]: Buffer | string | undefined
}
Nevon added a commit that referenced this issue Oct 16, 2020
Since the index signature previously claimed that the value
was always `Buffer | string` it means that Typescript would
build even when accessing a header key that isn't guaranteed to
be there, and thus crash at runtime unless a null-check is in place.

Fixes #924.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants