Skip to content

Commit

Permalink
Tweak tuple type emission for nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Mar 5, 2024
1 parent fd2cf31 commit fdf4a5b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/driver/src/reflection/analyzeQuery.ts
Expand Up @@ -149,7 +149,8 @@ export const defaultCodecGenerators: CodecGeneratorMap = new Map([
const subCodecs = codec
.getSubcodecs()
.map((subCodec) => ctx.walk(subCodec));
return `${ctx.readonly ? "readonly " : ""}[${subCodecs.join(", ")}]`;
const tuple = `[${subCodecs.join(", ")}]`;
return ctx.readonly ? `(readonly ${tuple})` : tuple;
}),
genDef(ArrayCodec, (codec, ctx) =>
ctx.applyCardinality(ctx.walk(codec.getSubcodecs()[0]), Cardinality.Many)
Expand Down Expand Up @@ -216,13 +217,15 @@ export const defaultApplyCardinalityToTsType =
(type: string, cardinality: Cardinality): string => {
switch (cardinality) {
case Cardinality.Many:
return `${ctx.readonly ? "readonly " : ""}${type}[]`;
return `${ctx.readonly ? "Readonly" : ""}Array<${type}>`;
case Cardinality.One:
return type;
case Cardinality.AtMostOne:
return `${type} | null`;
case Cardinality.AtLeastOne:
return `${ctx.readonly ? "readonly " : ""}[(${type}), ...(${type})[]]`;
case Cardinality.AtLeastOne: {
const tuple = `[(${type}), ...(${type})[]]`;
return ctx.readonly ? `(readonly ${tuple})` : tuple;
}
}
throw new Error(`Unexpected cardinality: ${cardinality}`);
};

0 comments on commit fdf4a5b

Please sign in to comment.