Skip to content

Commit fb9d0ff

Browse files
committed
Typescript doc tags
1 parent cb60228 commit fb9d0ff

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,25 @@ converter.Add(Person{})
222222

223223
The model name will be `API_Person` instead of `Person`.
224224

225+
## Field comments
226+
227+
Field documentation comments can be added with the `ts_doc` tag:
228+
229+
```golang
230+
type Person struct {
231+
Name string `json:"name" ts_doc:"This is a comment"`
232+
}
233+
```
234+
235+
Generated typescript:
236+
237+
```typescript
238+
export class Person {
239+
/** This is a comment */
240+
name: string;
241+
}
242+
```
243+
225244
## Custom types
226245

227246
If your field has a type not supported by typescriptify which can be JSONized as is, then you can use the `ts_type` tag to specify the typescript type to use:

typescriptify/typescriptify.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
const (
16+
tsDocTag = "ts_doc"
1617
tsTransformTag = "ts_transform"
1718
tsType = "ts_type"
1819
tsConvertValuesFunc = `convertValues(a: any, classs: any, asMap: boolean = false): any {
@@ -37,6 +38,7 @@ const (
3738
// TypeOptions overrides options set by `ts_*` tags.
3839
type TypeOptions struct {
3940
TSType string
41+
TSDoc string
4042
TSTransform string
4143
}
4244

@@ -477,7 +479,11 @@ func (t *TypeScriptify) convertEnum(depth int, typeOf reflect.Type, elements []e
477479

478480
func (t *TypeScriptify) getFieldOptions(structType reflect.Type, field reflect.StructField) TypeOptions {
479481
// By default use options defined by tags:
480-
opts := TypeOptions{TSTransform: field.Tag.Get(tsTransformTag), TSType: field.Tag.Get(tsType)}
482+
opts := TypeOptions{
483+
TSTransform: field.Tag.Get(tsTransformTag),
484+
TSType: field.Tag.Get(tsType),
485+
TSDoc: field.Tag.Get(tsDocTag),
486+
}
481487

482488
overrides := []TypeOptions{}
483489

@@ -579,6 +585,9 @@ func (t *TypeScriptify) convertType(depth int, typeOf reflect.Type, customCode m
579585

580586
var err error
581587
fldOpts := t.getFieldOptions(typeOf, field)
588+
if fldOpts.TSDoc != "" {
589+
result += "\t/** " + fldOpts.TSDoc + " */\n"
590+
}
582591
if fldOpts.TSTransform != "" {
583592
t.logf(depth, "- simple field %s.%s", typeOf.Name(), field.Name)
584593
err = builder.AddSimpleField(jsonFieldName, field, fldOpts)

typescriptify/typescriptify_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,22 @@ export class WithoutAnnotation {
990990
`
991991
testConverter(t, converter, true, desiredResult, nil)
992992
}
993+
994+
func TestTypescriptifyComment(t *testing.T) {
995+
t.Parallel()
996+
type Person struct {
997+
Name string `json:"name" ts_doc:"This is a comment"`
998+
}
999+
1000+
converter := New()
1001+
1002+
converter.AddType(reflect.TypeOf(Person{}))
1003+
converter.BackupDir = ""
1004+
converter.CreateConstructor = false
1005+
1006+
desiredResult := `export class Person {
1007+
/** This is a comment */
1008+
name: string;
1009+
}`
1010+
testConverter(t, converter, false, desiredResult, nil)
1011+
}

0 commit comments

Comments
 (0)