Skip to content

Commit

Permalink
Accept readonly arrays on assignment (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Mar 1, 2024
1 parent 18536d1 commit ebf6351
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
10 changes: 10 additions & 0 deletions integration-tests/lts/insert.test.ts
Expand Up @@ -391,6 +391,16 @@ describe("insert", () => {
await query.run(client);
});

test("readonly arrays for array and multi properties", async () => {
const items: readonly string[] = ["asdf"];
const query = e.insert(e.Bag, {
stringsMulti: ["asdf", ...items] as const,
stringMultiArr: [items] as const,
stringsArr: items,
});
await query.run(client);
});

test("insert named tuple as shape", async () => {
const query = e.params(
{
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/lts/params.test.ts
Expand Up @@ -100,10 +100,10 @@ SELECT (SELECT {
str: string;
numArr: readonly number[];
optBool?: boolean | null;
tuple: readonly [string, number, boolean[]];
namedTuple: Readonly<{ a: number; b: bigint[]; c: string }>;
tuple: readonly [string, number, readonly boolean[]];
namedTuple: Readonly<{ a: number; b: readonly bigint[]; c: string }>;
jsonTuple: readonly [unknown];
people: Readonly<{ name: string; age: number; tags: string[] }[]>;
people: Readonly<{ name: string; age: number; tags: readonly string[] }[]>;
}
>
>(true);
Expand Down
8 changes: 4 additions & 4 deletions packages/generate/src/syntax/casting.ts
Expand Up @@ -109,15 +109,15 @@ export type setToAssignmentExpression<
type getAssignmentLiteral<
Set extends PrimitiveTypeSet,
IsSetModifier extends boolean
> = BaseTypeToTsType<Set["__element__"]> extends infer TsType
> = BaseTypeToTsType<Set["__element__"], true> extends infer TsType
?
| TsType
| (Set["__cardinality__"] extends Cardinality.Many
? TsType[]
? readonly TsType[]
: Set["__cardinality__"] extends Cardinality.AtLeastOne
? IsSetModifier extends true
? TsType[]
: [TsType, ...TsType[]]
? readonly TsType[]
: readonly [TsType, ...TsType[]]
: never)
: never;

Expand Down
6 changes: 5 additions & 1 deletion packages/generate/src/syntax/typesystem.ts
Expand Up @@ -521,7 +521,11 @@ export interface ArrayType<
type ArrayTypeToTsType<
Type extends ArrayType,
isParam extends boolean = false
> = BaseTypeToTsType<Type["__element__"], isParam>[];
> = BaseTypeToTsType<Type["__element__"], isParam> extends infer TsType
? isParam extends true
? readonly TsType[]
: TsType[]
: never;

/////////////////////////
/// TUPLE TYPE
Expand Down

0 comments on commit ebf6351

Please sign in to comment.