Skip to content

Array of union type generates incorrect syntax: A | B | C[] instead of Array<A | B | C> #56

Description

Summary

When an Avro schema has an array whose items are a union type, avroToTypeScript generates incorrect TypeScript syntax. The [] suffix is appended after the last union member only, rather than wrapping the full union in Array<...>. The result is a type that TypeScript parses differently from what was intended.

Reproduction

Avro schema:

{
  "type": "record",
  "name": "Analysis",
  "fields": [
    {
      "name": "detections",
      "type": ["null", {
        "type": "array",
        "items": [
          { "type": "record", "name": "MotionDetection", "fields": [] },
          { "type": "record", "name": "PersonDetection", "fields": [] },
          { "type": "record", "name": "FaceDetection", "fields": [] }
        ]
      }],
      "default": null
    }
  ]
}

Generated output (incorrect):

export interface Analysis {
    detections: null | MotionDetection | PersonDetection | FaceDetection[];
}

Expected output:

export interface Analysis {
    detections: null | Array<MotionDetection | PersonDetection | FaceDetection>;
}

Why this is wrong

TypeScript parses null | MotionDetection | PersonDetection | FaceDetection[] as:

null  |  MotionDetection  |  PersonDetection  |  (FaceDetection[])

The array type applies only to FaceDetection, not to the full union. A detections value of type MotionDetection[] would be incorrectly accepted since MotionDetection (non-array) is a union member.

Root cause

In convertType, when isArrayType is true, the items type is converted recursively and [] is appended:

else if (isArrayType(type)) {
    return convertType(type.items, buffer, opts) + "[]";
}

When type.items is itself a union (an array in Avro schema), convertType returns a |-joined string and [] is appended to the last member only.

Fix

When items is a union (i.e. the result contains |), wrap in Array<...> instead of appending []:

else if (isArrayType(type)) {
    const itemType = convertType(type.items, buffer, opts);
    return itemType.includes(' | ') ? `Array<${itemType}>` : `${itemType}[]`;
}

Environment

  • avro-typescript version: 1.3.0
  • Node.js: 20.11.1

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions