Conversation
# Conflicts: # src/ts_generator/sql_parser/expressions/translate_expr.rs
There was a problem hiding this comment.
Pull request overview
This PR continues the effort from #244 to prefer unknown over any in generated TypeScript types, updating the Rust type-mapping logic and refreshing the demo/test fixtures and snapshots to match.
Changes:
- Introduces
TsFieldType::Unknownand switches several “fallback”/untyped mappings fromanytounknown. - Updates SQL expression and data type translation paths to emit
unknownin more cases. - Regenerates/updates demo query outputs and snapshots to reflect
unknowntypes and renamed query artifacts.
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/demo_json/postgres/array_operations.ts | Updates demo query name for array literal case; now intended to reflect unknown. |
| tests/demo_json/postgres/array_operations.snapshot.ts | Updates generated snapshot types from any to unknown and renames ArrayLiteral* types. |
| tests/demo_json/postgres/array_operations.queries.ts | Updates generated query types from any to unknown and renames ArrayLiteral* types. |
| tests/demo/typescript/expression/types.ts | Updates demo TypeScript types to use unknown instead of any. |
| tests/demo/typescript/expression/as_const.ts | Changes Record<string, any> to Record<string, unknown> in a demo fixture. |
| tests/demo/select/select.snapshot.ts | Updates snapshot field type(s) from any to unknown. |
| tests/demo/select/select.queries.ts | Updates query field type(s) from any to unknown. |
| tests/demo/select/no-default-table.snapshot.ts | Updates multiple JSON-ish snapshot fields from any to unknown. |
| tests/demo/select/no-default-table.queries.ts | Updates multiple JSON-ish query fields from any to unknown. |
| tests/demo/datetime_functions/time_functions.snapshot.ts | Updates snapshot time/interval-related fields from any to unknown. |
| tests/demo/datetime_functions/time_functions.queries.ts | Updates query time/interval-related fields from any to unknown. |
| tests/demo/datetime_functions/date_functions.snapshot.ts | Updates snapshot date arithmetic field from any to unknown. |
| tests/demo/datetime_functions/date_functions.queries.ts | Updates query date arithmetic field from any to unknown. |
| tests/demo/case_expressions/simple_case.snapshot.ts | Updates CASE expression snapshot outputs from any to unknown. |
| tests/demo/case_expressions/simple_case.queries.ts | Updates CASE expression query outputs from any to unknown. |
| tests/demo/case_expressions/searched_case.snapshot.ts | Updates searched CASE snapshot outputs from any to unknown. |
| tests/demo/case_expressions/searched_case.queries.ts | Updates searched CASE query outputs from any to unknown. |
| tests/demo/case_expressions/nested_case.snapshot.ts | Updates nested CASE snapshot outputs from any to unknown. |
| tests/demo/case_expressions/nested_case.queries.ts | Updates nested CASE query outputs from any to unknown. |
| tests/demo/case_expressions/case_in_clauses.snapshot.ts | Updates CASE-in-clause snapshot output from any to unknown. |
| tests/demo/case_expressions/case_in_clauses.queries.ts | Updates CASE-in-clause query output from any to unknown. |
| tests/demo/aggregate_functions/basic_aggregates.snapshot.ts | Updates aggregate snapshot outputs from any to unknown. |
| tests/demo/aggregate_functions/basic_aggregates.queries.ts | Updates aggregate query outputs from any to unknown. |
| src/ts_generator/types/ts_query.rs | Adds Unknown variant and shifts multiple default mappings from Any to Unknown. |
| src/ts_generator/sql_parser/expressions/translate_expr.rs | Replaces multiple expression-result fallbacks from Any to Unknown. |
| src/ts_generator/sql_parser/expressions/translate_data_type.rs | Switches several data type translations from Any to Unknown. |
Comments suppressed due to low confidence (1)
src/ts_generator/types/ts_query.rs:123
TsFieldType::Unknownis added and most default/fallback mappings in this file now returnUnknown, butfrom_sqlparser_datatype()(later in this file) still defaults toAny(includingDataType::Array(_) => Self::Anyand the final wildcard arm). If the goal is to eliminateanyfor unknown/unhandled types, consider aligning those fallbacks toUnknownas well so alias column definitions don’t reintroduceanyinto generated types.
Any,
Unknown,
#[allow(dead_code)]
Array2D(Array2DContent),
Array(Box<TsFieldType>),
#[allow(dead_code)]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const arrayLiteralAny = sql` | ||
| -- @name: array literal any | ||
| // Array literal and unknown | ||
| const arrayLiteralunknown = sql` |
There was a problem hiding this comment.
arrayLiteralunknown uses inconsistent casing (likely a typo) compared to other camelCase identifiers in this fixture (e.g., arrayAggWithOrderBy, arrayWithParams). Renaming to arrayLiteralUnknown would keep naming consistent and easier to grep alongside the -- @name: array literal unknown annotation.
| const arrayLiteralunknown = sql` | |
| const arrayLiteralUnknown = sql` |
| let name: any = 'test' | ||
| let companyName = <string>name | ||
| let name: unknown = 'test' | ||
| let compunknownName = <string>name |
There was a problem hiding this comment.
compunknownName looks like an unintended rename/typo (it reads like companyName with unknown inserted). Consider renaming it to a clearer identifier (e.g., companyName or compUnknownName) to avoid confusion in this TypeScript expression fixture.
| let compunknownName = <string>name | |
| let compUnknownName = <string>name |
| Expr::JsonAccess { value: _, path: _ } => { | ||
| ts_query.insert_result(alias, &[TsFieldType::Any], is_selection, false, expr_for_logging)?; | ||
| ts_query.insert_param(&TsFieldType::Any, &false, &None) | ||
| ts_query.insert_result(alias, &[TsFieldType::Unknown], is_selection, false, expr_for_logging)?; | ||
| ts_query.insert_param(&TsFieldType::Unknown, &false, &None) | ||
| } |
There was a problem hiding this comment.
This PR replaces many Any fallbacks with Unknown, but Expr::Value still falls back to TsFieldType::Any when it can’t infer a literal’s type (see the inferred_type assignment just above this hunk). If the intent is to avoid emitting any for unknown types, consider switching that fallback to TsFieldType::Unknown as well (keeping the Boolean special-case if needed).
| let args = match &func_obj.args { | ||
| FunctionArguments::List(arg_list) => &arg_list.args, | ||
| _ => { | ||
| // If no arguments or subquery, return Any | ||
| return ts_query.insert_result(Some(alias), &[TsFieldType::Any], is_selection, false, expr_for_logging); | ||
| return ts_query.insert_result( | ||
| Some(alias), | ||
| &[TsFieldType::Unknown], | ||
| is_selection, | ||
| false, | ||
| expr_for_logging, | ||
| ); |
There was a problem hiding this comment.
In this file, the fallback for is_json_agg_function when there are no arguments/subquery was updated to Unknown, but the analogous fallback for is_json_build_function (just above) still returns TsFieldType::Any. To consistently avoid emitting any for unknown function return types, consider switching the json-build fallback to Unknown as well.
Continuing on #244...
rebasing with main branch