Skip to content

Commit e3fee04

Browse files
committed
fix(lint): eliminate 5 bare as casts to satisfy lint:casts ratchet
CI lint:casts reported +5 net new bare casts vs merge-base: - 3 in relational-core/src/expression.ts (createRawSql factory) - 1 in sql-builder/src/runtime/joined-tables-impl.ts (#addJoin) - 1 in sql-builder/src/runtime/query-impl.ts (having()) Refactor each site to remove the cast without weakening typing: - expression.ts: rewrite createRawSql to use values.forEach (eliminates the values[i] cast and preserves the defence-in-depth throw on undefined interpolations); move the builder into a RawSqlBuilderImpl class so the overloaded returns() signatures live next to the wider implementation signature (no per-call casts needed). - joined-tables-impl.ts: drop the redundant 'fns as Functions<QC>' cast (createFunctions<QC>() already returns the exact type) and prune the now-unused Functions import. - query-impl.ts: pass <QC> explicitly to createAggregateFunctions so its return type is AggregateFunctions<QC> without needing a cast. Signed-off-by: Serhii Tatarintsev <tatarintsev@prisma.io>
1 parent 7fa065b commit e3fee04

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

packages/2-sql/4-lanes/relational-core/src/expression.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,33 @@ function resolveInterpolation(
228228
export function createRawSql(adapter: RawCodecInferer): RawSqlTag {
229229
return (strings, ...values) => {
230230
const parts: (string | AstExpression | ParamRef)[] = [];
231-
for (let i = 0; i < strings.length; i++) {
232-
const fragment = strings[i] ?? '';
233-
parts.push(fragment);
234-
if (i < values.length) {
235-
parts.push(resolveInterpolation(adapter, values[i] as RawSqlInterpolation));
236-
}
237-
}
238-
return {
239-
returns(spec: string | { readonly codecId: string; readonly nullable?: boolean }) {
240-
const codecId = typeof spec === 'string' ? spec : spec.codecId;
241-
const nullable = typeof spec === 'string' ? false : (spec.nullable ?? false);
242-
const returns: ParamSpec = { codecId, nullable };
243-
const node = new RawExpr({ parts, returns });
244-
return {
245-
returnType: { codecId, nullable },
246-
buildAst: () => node,
247-
} as Expression<{ codecId: string; nullable: boolean }>;
248-
},
249-
} as RawSqlBuilder;
231+
parts.push(strings[0] ?? '');
232+
values.forEach((value, i) => {
233+
parts.push(resolveInterpolation(adapter, value));
234+
parts.push(strings[i + 1] ?? '');
235+
});
236+
return new RawSqlBuilderImpl(parts);
250237
};
251238
}
239+
240+
class RawSqlBuilderImpl implements RawSqlBuilder {
241+
constructor(private readonly parts: readonly (string | AstExpression | ParamRef)[]) {}
242+
243+
returns<S extends string>(spec: S): Expression<{ codecId: S; nullable: false }>;
244+
returns<S extends string, N extends boolean = false>(spec: {
245+
readonly codecId: S;
246+
readonly nullable?: N;
247+
}): Expression<{ codecId: S; nullable: N }>;
248+
returns(
249+
spec: string | { readonly codecId: string; readonly nullable?: boolean },
250+
): Expression<{ codecId: string; nullable: boolean }> {
251+
const codecId = typeof spec === 'string' ? spec : spec.codecId;
252+
const nullable = typeof spec === 'string' ? false : (spec.nullable ?? false);
253+
const paramSpec: ParamSpec = { codecId, nullable };
254+
const node = new RawExpr({ parts: this.parts, returns: paramSpec });
255+
return {
256+
returnType: { codecId, nullable },
257+
buildAst: () => node,
258+
};
259+
}
260+
}

packages/2-sql/4-lanes/sql-builder/src/runtime/joined-tables-impl.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
ExpressionBuilder,
1111
ExtractScopeFields,
1212
FieldProxy,
13-
Functions,
1413
WithField,
1514
WithFields,
1615
} from '../expression';
@@ -179,7 +178,7 @@ export class JoinedTablesImpl<QC extends QueryContext, AvailableScope extends Sc
179178
),
180179
) as FieldProxy<MergeScopes<AvailableScope, Other[typeof JoinOuterScope]>>;
181180
const fns = createFunctions<QC>(this.ctx.queryOperationTypes, this.ctx.rawCodecInferer);
182-
const onResult = onExpr(fieldProxy, fns as Functions<QC>);
181+
const onResult = onExpr(fieldProxy, fns);
183182
const joinAst = new JoinAst(joinType, other.buildAst(), onResult.buildAst());
184183

185184
return new JoinedTablesImpl(

packages/2-sql/4-lanes/sql-builder/src/runtime/query-impl.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ export class GroupedQueryImpl<
269269
this.state.scope as AvailableScope,
270270
this.state.rowFields as RowType,
271271
);
272-
const fns = createAggregateFunctions(this.ctx.queryOperationTypes, this.ctx.rawCodecInferer);
273-
const result = expr(createFieldProxy(combined), fns as AggregateFunctions<QC>);
272+
const fns = createAggregateFunctions<QC>(
273+
this.ctx.queryOperationTypes,
274+
this.ctx.rawCodecInferer,
275+
);
276+
const result = expr(createFieldProxy(combined), fns);
274277
return new GroupedQueryImpl(cloneState(this.state, { having: result.buildAst() }), this.ctx);
275278
}
276279

0 commit comments

Comments
 (0)