From e3f088982ca5fca6445959a632efdcb520eecaae Mon Sep 17 00:00:00 2001 From: Dan Hughes Date: Mon, 3 Feb 2025 17:55:30 +0000 Subject: [PATCH 1/4] Add test demonstrating inconsistent map behaviour --- typescriptify/typescriptify_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/typescriptify/typescriptify_test.go b/typescriptify/typescriptify_test.go index bde6225..cd6cb75 100644 --- a/typescriptify/typescriptify_test.go +++ b/typescriptify/typescriptify_test.go @@ -1076,3 +1076,26 @@ func TestTypescriptifyCustomJsonTag(t *testing.T) { }` testConverter(t, converter, false, desiredResult, nil) } + +func TestTypescriptifyCustomTypeInMap(t *testing.T) { + converter := New().WithInterface(true) + + type Foo uint + converter.ManageType(reflect.TypeOf(Foo(0)), TypeOptions{TSType: "number"}) + + type Bar struct { + Foo Foo `json:"foo"` + FooInKey map[Foo]bool `json:"fooInKey"` + FooInValue map[string]Foo `json:"fooInValue"` + } + + converter.AddType(reflect.TypeOf(Bar{})) + + desiredResult := `export interface Bar { + foo: number; + fooInKey: {[key: number]: boolean}; + fooInValue: {[key: string]: number}; + }` + + testConverter(t, converter, true, desiredResult, nil) +} From b3fb79e74ebd152769781552d435b38fc08d4889 Mon Sep 17 00:00:00 2001 From: Dan Hughes Date: Mon, 3 Feb 2025 17:57:21 +0000 Subject: [PATCH 2/4] use a class instead of interfaces --- typescriptify/typescriptify_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/typescriptify/typescriptify_test.go b/typescriptify/typescriptify_test.go index cd6cb75..117815f 100644 --- a/typescriptify/typescriptify_test.go +++ b/typescriptify/typescriptify_test.go @@ -1078,7 +1078,8 @@ func TestTypescriptifyCustomJsonTag(t *testing.T) { } func TestTypescriptifyCustomTypeInMap(t *testing.T) { - converter := New().WithInterface(true) + converter := New() + converter.CreateConstructor = false type Foo uint converter.ManageType(reflect.TypeOf(Foo(0)), TypeOptions{TSType: "number"}) @@ -1091,11 +1092,11 @@ func TestTypescriptifyCustomTypeInMap(t *testing.T) { converter.AddType(reflect.TypeOf(Bar{})) - desiredResult := `export interface Bar { - foo: number; - fooInKey: {[key: number]: boolean}; - fooInValue: {[key: string]: number}; - }` + desiredResult := `export class Bar { + foo: number; + fooInKey: {[key: number]: boolean}; + fooInValue: {[key: string]: number}; +}` testConverter(t, converter, true, desiredResult, nil) } From ae2b6d64c85c8afe37d0b4724e118ee74387dc22 Mon Sep 17 00:00:00 2001 From: Dan Hughes Date: Mon, 3 Feb 2025 18:17:16 +0000 Subject: [PATCH 3/4] Try using the mapped type returned from the array --- typescriptify/typescriptify.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/typescriptify/typescriptify.go b/typescriptify/typescriptify.go index 525ee46..183eb9a 100644 --- a/typescriptify/typescriptify.go +++ b/typescriptify/typescriptify.go @@ -272,12 +272,13 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str } strippedFieldName := strings.ReplaceAll(fieldName, "?", "") - keyTypeStr := keyType.Name() - // Key should always be string, no need for this: - // _, isSimple := t.types[keyType.Kind()] - // if !isSimple { - // keyTypeStr = t.prefix + keyType.Name() + t.suffix - // } + var keyTypeStr string + mappedType, isSimple := t.types[keyType.Kind()] + if !isSimple { + keyTypeStr = t.prefix + keyType.Name() + t.suffix + } else { + keyTypeStr = t.prefix + mappedType + t.suffix + } if valueType.Kind() == reflect.Struct { t.fields = append(t.fields, fmt.Sprintf("%s%s: {[key: %s]: %s};", t.indent, fieldName, keyTypeStr, t.prefix+valueTypeName)) From 55861f21709c419015f42a1d508de82f9369e053 Mon Sep 17 00:00:00 2001 From: Dan Hughes Date: Mon, 3 Feb 2025 18:20:49 +0000 Subject: [PATCH 4/4] Non-strict test --- typescriptify/typescriptify_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescriptify/typescriptify_test.go b/typescriptify/typescriptify_test.go index 117815f..013bde9 100644 --- a/typescriptify/typescriptify_test.go +++ b/typescriptify/typescriptify_test.go @@ -1098,5 +1098,5 @@ func TestTypescriptifyCustomTypeInMap(t *testing.T) { fooInValue: {[key: string]: number}; }` - testConverter(t, converter, true, desiredResult, nil) + testConverter(t, converter, false, desiredResult, nil) }