fix🐛: getVerifyXxx's pk parameter type follows GoType, not always number

ts.go.template hardcoded the pk parameter as `: number`. vue.go.template
(F4, landed after this file) derives $pkType per table - "number" unless
the pk column's GoType is "string" (natural keys do exist; sys_tables.go
gives a pk column GoType "string" whenever its ColumnType is not
int-shaped), then types useForm on that same $pkType. A string-pk table
therefore generated a page calling get{Class}(id: string) against an api
module whose get{Class} only accepted number - a TS compile error, not
a runtime bug, and exactly the kind of gap each side's own template-only
verification could not see (F4 was checked against a hand-written .ts
stub before this template existed; this template was checked against a
numeric-pk fixture only).

Copies vue.go.template's $pkType derivation verbatim so both templates
agree by construction rather than by convention.

Verified: generated both a numeric-pk and a string-pk table's api module,
copied both into go-admin-ui and ran vue-tsc --noEmit - clean. Added a
throwaway call-site file exercising getVerifyStrPk with a string and (via
@ts-expect-error) confirming a number argument is now rejected, so the
type is actually enforced rather than having quietly widened to any.
This commit is contained in:
zhangwenjian
2026-09-19 14:12:01 +08:00
parent d65b21baf6
commit c3d2a5952b
+14 -1
View File
@@ -1,3 +1,16 @@
{{- /*
$pkType: the primary key's TS type for get{ClassName}'s parameter.
Defaults to "number" - true for every column but string primary keys
(natural keys), which do exist (sys_tables.go:323-338 gives a primary
key column GoType "string" whenever its ColumnType is not int-shaped).
Matches vue.go.template's own $pkType derivation exactly (F4) - useForm
there is typed on the same column, and a mismatch between the two is a
TS compile error at the call site, not a runtime bug.
*/ -}}
{{- $pkType := "number" -}}
{{- range .Columns -}}
{{- if and .Pk (eq .GoType "string") }}{{$pkType = "string"}}{{end -}}
{{- end -}}
import request from '@/utils/request'
import type { ApiResponse, PageQuery, PageResult, Id } from '@/types/api'
@@ -43,7 +56,7 @@ export function list{{.ClassName}}(query: {{.ClassName}}Query & PageQuery) {
})
}
export function get{{.ClassName}}({{.PkJsonField}}: number) {
export function get{{.ClassName}}({{.PkJsonField}}: {{$pkType}}) {
return request<ApiResponse<{{.ClassName}}>>({
url: '/api/v1/{{.ModuleName}}/' + {{.PkJsonField}},
method: 'get'