From 233377b8545877a592899e705ddaa00a0c6dd889 Mon Sep 17 00:00:00 2001 From: Igal Klebanov Date: Sun, 30 Oct 2022 12:51:56 +0200 Subject: [PATCH] `exactOptionalPropertyTypes` should no longer error when updating/inserting with undefined values in nullable columns. (#210) * make `exactOptionalPropertyTypes` chill the f out. * `exactOptionalPropertyTypes` cases in typings tests. --- src/parser/insert-values-parser.ts | 8 +++----- src/parser/update-set-parser.ts | 4 +++- test/typings/test-d/index.test-d.ts | 24 +++++++++++++++++++++++- test/typings/tsconfig.json | 1 + 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/parser/insert-values-parser.ts b/src/parser/insert-values-parser.ts index 66efe01ee..b9e7259f4 100644 --- a/src/parser/insert-values-parser.ts +++ b/src/parser/insert-values-parser.ts @@ -19,11 +19,9 @@ export type InsertObject = { InsertType > } & { - [C in NullableInsertKeys]?: ValueExpression< - DB, - TB, - InsertType - > + [C in NullableInsertKeys]?: + | ValueExpression> + | undefined } export type InsertObjectOrList = diff --git a/src/parser/update-set-parser.ts b/src/parser/update-set-parser.ts index cd00624ca..80d43f6e5 100644 --- a/src/parser/update-set-parser.ts +++ b/src/parser/update-set-parser.ts @@ -4,7 +4,9 @@ import { UpdateKeys, UpdateType } from '../util/column-type.js' import { parseValueExpression, ValueExpression } from './value-parser.js' export type MutationObject = { - [C in UpdateKeys]?: ValueExpression> + [C in UpdateKeys]?: + | ValueExpression> + | undefined } export function parseUpdateObject( diff --git a/test/typings/test-d/index.test-d.ts b/test/typings/test-d/index.test-d.ts index 13e310214..7e2dc47e5 100644 --- a/test/typings/test-d/index.test-d.ts +++ b/test/typings/test-d/index.test-d.ts @@ -693,9 +693,24 @@ async function testInsert(db: Kysely) { db.insertInto('person').values({ first_name: 'what', gender: 'male', - age: (eb) => eb.selectFrom('pet').select('pet.name') + age: (eb) => eb.selectFrom('pet').select('pet.name'), }) ) + + // Nullable column as undefined + const insertObject: { + first_name: string + last_name: string | undefined + age: number + gender: 'male' | 'female' | 'other' + } = { + first_name: 'emily', + last_name: 'smith', + age: 25, + gender: 'female', + } + + db.insertInto('person').values(insertObject) } async function testReturning(db: Kysely) { @@ -789,6 +804,13 @@ async function testUpdate(db: Kysely) { expectError(db.updateTable('book').set({ id: 1, name: 'foo' })) db.updateTable('book').set({ name: 'bar' }) + + // Nullable column as undefined + const mutationObject: { last_name: string | undefined } = { + last_name: 'smith', + } + + db.updateTable('person').set(mutationObject) } async function testDelete(db: Kysely) { diff --git a/test/typings/tsconfig.json b/test/typings/tsconfig.json index 3d4ddedcd..95229618a 100644 --- a/test/typings/tsconfig.json +++ b/test/typings/tsconfig.json @@ -2,6 +2,7 @@ "extends": "../../tsconfig-base.json", "include": ["./**/*"], "compilerOptions": { + "exactOptionalPropertyTypes": true, "module": "CommonJS" } }