Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept readonly arrays on assignment #884

Merged
merged 5 commits into from Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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[]
CarsonF marked this conversation as resolved.
Show resolved Hide resolved
: 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