Skip to content

Commit

Permalink
exactOptionalPropertyTypes should no longer error when updating/ins…
Browse files Browse the repository at this point in the history
…erting with undefined values in nullable columns. (#210)

* make `exactOptionalPropertyTypes` chill the f out.

* `exactOptionalPropertyTypes` cases in typings tests.
  • Loading branch information
igalklebanov authored Oct 30, 2022
1 parent 3f91374 commit 233377b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/parser/insert-values-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export type InsertObject<DB, TB extends keyof DB> = {
InsertType<DB[TB][C]>
>
} & {
[C in NullableInsertKeys<DB[TB]>]?: ValueExpression<
DB,
TB,
InsertType<DB[TB][C]>
>
[C in NullableInsertKeys<DB[TB]>]?:
| ValueExpression<DB, TB, InsertType<DB[TB][C]>>
| undefined
}

export type InsertObjectOrList<DB, TB extends keyof DB> =
Expand Down
4 changes: 3 additions & 1 deletion src/parser/update-set-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { UpdateKeys, UpdateType } from '../util/column-type.js'
import { parseValueExpression, ValueExpression } from './value-parser.js'

export type MutationObject<DB, TB extends keyof DB, TM extends keyof DB> = {
[C in UpdateKeys<DB[TM]>]?: ValueExpression<DB, TB, UpdateType<DB[TM][C]>>
[C in UpdateKeys<DB[TM]>]?:
| ValueExpression<DB, TB, UpdateType<DB[TM][C]>>
| undefined
}

export function parseUpdateObject(
Expand Down
24 changes: 23 additions & 1 deletion test/typings/test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,24 @@ async function testInsert(db: Kysely<Database>) {
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<Database>) {
Expand Down Expand Up @@ -789,6 +804,13 @@ async function testUpdate(db: Kysely<Database>) {
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<Database>) {
Expand Down
1 change: 1 addition & 0 deletions test/typings/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig-base.json",
"include": ["./**/*"],
"compilerOptions": {
"exactOptionalPropertyTypes": true,
"module": "CommonJS"
}
}

0 comments on commit 233377b

Please sign in to comment.