From 6111d0e6733802d63d2c0a1b80889987f78bc91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Koskim=C3=A4ki?= Date: Sat, 22 Jul 2023 09:28:49 +0300 Subject: [PATCH] further type speedup and simplification --- src/parser/select-parser.ts | 24 ++++++++++++++---------- src/util/column-type.ts | 28 +++++++++++++++++----------- src/util/type-utils.ts | 22 +++++++++++++--------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/parser/select-parser.ts b/src/parser/select-parser.ts index f48a02d09..ce46a7801 100644 --- a/src/parser/select-parser.ts +++ b/src/parser/select-parser.ts @@ -42,11 +42,13 @@ export type SelectArg< | ReadonlyArray | ((eb: ExpressionBuilder) => ReadonlyArray) -export type Selection = { - [A in ExtractAliasFromSelectExpression]: SelectType< - ExtractTypeFromSelectExpression - > -} +export type Selection = [SE] extends [unknown] + ? { + [A in ExtractAliasFromSelectExpression]: SelectType< + ExtractTypeFromSelectExpression + > + } + : never type ExtractAliasFromSelectExpression = SE extends string ? ExtractAliasFromStringSelectExpression @@ -151,11 +153,13 @@ type ExtractTypeFromStringSelectExpression< : never : never -export type AllSelection = Selectable<{ - [C in AnyColumn]: { - [T in TB]: C extends keyof DB[T] ? DB[T][C] : never - }[TB] -}> +export type AllSelection = [DB] extends [unknown] + ? Selectable<{ + [C in AnyColumn]: { + [T in TB]: C extends keyof DB[T] ? DB[T][C] : never + }[TB] + }> + : never export function parseSelectArg( selection: SelectArg> diff --git a/src/util/column-type.ts b/src/util/column-type.ts index cdbf0d5d5..c52cb3dd7 100644 --- a/src/util/column-type.ts +++ b/src/util/column-type.ts @@ -149,9 +149,11 @@ export type UpdateKeys = { * // } * ``` */ -export type Selectable = { - [K in NonNeverSelectKeys]: SelectType -} +export type Selectable = [R] extends [unknown] + ? { + [K in NonNeverSelectKeys]: SelectType + } + : never /** * Given a table interface, extracts the insert type from all @@ -174,11 +176,13 @@ export type Selectable = { * // } * ``` */ -export type Insertable = { - [K in NonNullableInsertKeys]: InsertType -} & { - [K in NullableInsertKeys]?: InsertType -} +export type Insertable = [R] extends [unknown] + ? { + [K in NonNullableInsertKeys]: InsertType + } & { + [K in NullableInsertKeys]?: InsertType + } + : never /** * Given a table interface, extracts the update type from all @@ -200,6 +204,8 @@ export type Insertable = { * // } * ``` */ -export type Updateable = { - [K in UpdateKeys]?: UpdateType -} +export type Updateable = [R] extends [unknown] + ? { + [K in UpdateKeys]?: UpdateType + } + : never diff --git a/src/util/type-utils.ts b/src/util/type-utils.ts index c1d35408c..46028f50a 100644 --- a/src/util/type-utils.ts +++ b/src/util/type-utils.ts @@ -35,19 +35,23 @@ import { KyselyTypeError } from './type-error.js' * // Columns == 'id' | 'name' | 'species' * ``` */ -export type AnyColumn = DrainOuterGeneric< - { - [T in TB]: keyof DB[T] - }[TB] & - string -> +export type AnyColumn = [DB] extends [unknown] + ? { + [T in TB]: keyof DB[T] + }[TB] & + string + : never /** * Extracts a column type. */ -export type ExtractColumnType = { - [T in TB]: C extends keyof DB[T] ? DB[T][C] : never -}[TB] +export type ExtractColumnType = [C] extends [ + unknown +] + ? { + [T in TB]: C extends keyof DB[T] ? DB[T][C] : never + }[TB] + : never /** * Given a database type and a union of table names in that db, returns