avro-typescript can be used to generate types mostly compatible with avsc. With unions, however, avsc often requires wrapped unions when there is ambiguity between the different union options. A wrapped union is an object with a single key, which contains the type name, and a single value, the actual value. Here's a comparison between unwrapped and wrapped unions:
- Unwrapped:
unionEnum: null | string | numberEnumType;
- Wrapped:
unionEnum: { "null": null } | { "string": string } | { "numberEnumType": numberEnumType };
This pattern allows avsc to disambiguate between values that are similar, such as numbers that might be int or double. In JS they are just number, so avsc can't tell the difference when faced with such values at runtime. This problem applies to records as well; in theory different record types can often be disambiguated, but a general algorithm is complicated (see this comment) and avsc hasn't implemented it. avsc docs on wrapEnums are here.
avro-typescript generates only unwrapped unions, but when the schema has ambiguous unions, avsc requires wrapped unions and the types don't match. At least one other user has encountered this problem as well; see this issue.
Proposal: add a new option wrapEnums which would cause avro-typescript to generate wrapped union types.
avro-typescript can be used to generate types mostly compatible with avsc. With unions, however, avsc often requires wrapped unions when there is ambiguity between the different union options. A wrapped union is an object with a single key, which contains the type name, and a single value, the actual value. Here's a comparison between unwrapped and wrapped unions:
unionEnum: null | string | numberEnumType;unionEnum: { "null": null } | { "string": string } | { "numberEnumType": numberEnumType };This pattern allows avsc to disambiguate between values that are similar, such as numbers that might be
intordouble. In JS they are justnumber, so avsc can't tell the difference when faced with such values at runtime. This problem applies to records as well; in theory different record types can often be disambiguated, but a general algorithm is complicated (see this comment) and avsc hasn't implemented it. avsc docs onwrapEnumsare here.avro-typescript generates only unwrapped unions, but when the schema has ambiguous unions, avsc requires wrapped unions and the types don't match. At least one other user has encountered this problem as well; see this issue.
Proposal: add a new option
wrapEnumswhich would cause avro-typescript to generate wrapped union types.