From c3d2a5952b9758ee38250797d4ea0667c5a6d34b Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 19 Sep 2026 14:12:01 +0800 Subject: [PATCH] =?UTF-8?q?fix=F0=9F=90=9B:=20getVerifyXxx's=20pk=20parame?= =?UTF-8?q?ter=20type=20follows=20GoType,=20not=20always=20number?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- template/v4/ts.go.template | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/template/v4/ts.go.template b/template/v4/ts.go.template index f6f62f65..7b1ed598 100644 --- a/template/v4/ts.go.template +++ b/template/v4/ts.go.template @@ -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>({ url: '/api/v1/{{.ModuleName}}/' + {{.PkJsonField}}, method: 'get'