Skip to content

Commit

Permalink
fix: allow SerializableValueType to include Record types with optiona…
Browse files Browse the repository at this point in the history
…l fields (fixes #222) (#223)
  • Loading branch information
adevine committed Oct 30, 2020
1 parent 46e4b8f commit 849453b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@typescript-eslint/no-non-null-assertion": 1,
"@typescript-eslint/prefer-ts-expect-error": 1,
"unicorn/prevent-abbreviations": 0,
"unicorn/numeric-separators-style": 0,
"no-undef": 0,
"linebreak-style": 0,
// https://github.com/typescript-eslint/typescript-eslint/issues/1624#issuecomment-589731039
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type SerializableValueType =
| boolean
| null
| {
[key: string]: SerializableValueType;
[key: string]: SerializableValueType | undefined;
}
| ReadonlyArray<SerializableValueType>;

Expand Down
15 changes: 15 additions & 0 deletions test/slonik/templateTags/sql/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ test('the resulting object is immutable', (t) => {
token.foo = 'bar';
});
});

test('Object types with optional properties are allowed', (t) => {
type TypeWithOptionalProperty = { foo: string; opt?: string };
const testValue: TypeWithOptionalProperty = {foo: 'bar'};

const query = sql`SELECT ${sql.json(testValue)}`;

t.deepEqual(query, {
sql: 'SELECT $1',
type: SqlToken,
values: [
'{"foo":"bar"}',
],
});
});

0 comments on commit 849453b

Please sign in to comment.