Skip to content

Typescript: FetchOffsetsPartition.error runtime/type mismatch — populated as null, declared as LibrdKafkaError | undefined #489

Closed

Description

Summary

@confluentinc/kafka-javascript's admin.fetchOffsets(...) resolves per-partition entries whose error field is populated as null at runtime when no error occurred, but the type declaration in types/kafkajs.d.ts declares the field as optional (error?: LibrdKafkaError), which TypeScript narrows to LibrdKafkaError | undefined.
The runtime value (null) and the type's view of the runtime value (undefined or absent) disagree.
Consumer code that writes the natural TypeScript-narrowing check if (partition.error !== undefined) { partition.error.code } then crashes with TypeError: Cannot read properties of null (reading 'code') because null !== undefined is true and the dereference goes through.

Type declaration

types/kafkajs.d.ts:

export type FetchOffsetsPartition = PartitionOffset & {
  metadata: string | null;
  leaderEpoch: number | null;
  error?: LibrdKafkaError;
};

TypeScript narrows partition.error to LibrdKafkaError | undefined.
The natural defensive checks partition.error !== undefined and partition.error == null (the latter via strictNullChecks narrowing) are equivalent under this type — both should suffice to guard a dereference.

Actual runtime

Calling admin.fetchOffsets({ groupId }) against a real Confluent Cloud Kafka cluster (kafkajs-compat surface, library version 1.x as of mid-2026) returns entries shaped like:

{
  topic: "orders",
  partitions: [
    { partition: 0, offset: "80", metadata: null, leaderEpoch: null, error: null },
    { partition: 1, offset: "-1", metadata: null, leaderEpoch: null, error: null },
  ],
}

Note error: null, not error: undefined or the key being absent.

Why this bites consumers

A correctness-minded consumer wants to distinguish "partition has a broker-reported error" (lag-unknown — should be surfaced separately from a happily-zero lag) from "partition reported a real offset". The TypeScript-natural check is:

if (partition.error !== undefined) {
  // partition.error is narrowed to LibrdKafkaError
  log(partition.error.code, partition.error.message);
}

Under the declared type this is correct. Under the actual runtime, the branch fires on error: null and then partition.error.code throws.

The workaround in consumer code is to use the loose-equality idiom partition.error != null (which catches both null and undefined), but this requires the consumer to know that the type lies — there's no signal in the type itself that points at this gotcha.

Suggested fixes

Either:

  1. Widen the type to honestly describe the runtime:

    error?: LibrdKafkaError | null;

    This makes TypeScript narrow partition.error to LibrdKafkaError | null | undefined, forcing consumers to handle the null case (either explicitly or via the loose != null check). Lowest-risk change; aligns the type with the existing runtime.

  2. Change the runtime to omit the error field (or set it to undefined) on successful partitions, matching the declared type. Higher-risk because it changes the wire shape, and any downstream consumer that does 'error' in partition (or iterates Object.keys) would observe a different shape.

(1) is the safer fix.

Reproduction

const { Kafka } = require("@confluentinc/kafka-javascript").KafkaJS;
const kafka = new Kafka({
  /* config */
});
const admin = kafka.admin();
await admin.connect();
const result = await admin.fetchOffsets({ groupId: "my-existing-group" });
for (const { partitions } of result) {
  for (const partition of partitions) {
    console.log(
      partition.error,
      partition.error === null,
      partition.error === undefined,
    );
  }
}
// Outputs: null true false  (when the partition has no error)

Expected per the .d.ts type:

undefined false true

or

LibrdKafkaError-instance false false

References

  • Sibling type/runtime mismatch on MemberDescription.assignment: #487.
    Same pattern (the .d.ts claims a flat TopicPartition[] but the runtime returns { topicPartitions: TopicPartition[] }).
  • The related upstream fetchTopicMetadata shape mismatch is tracked at #367 (response shape: bare array vs { topics: [...] }).
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