Skip to content

feat: golang map types unioned with possible 'null' #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions config/mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,39 @@ func (v *nullUnionVisitor) Visit(node bindings.Node) walk.Visitor {

return v
}

// NotNullMaps assumes all maps will not be null.
// Example:
// GolangType: map[string]string
// TsType: Record<string,string> | null --> Record<string,string>
func NotNullMaps(ts *guts.Typescript) {
ts.ForEach(func(key string, node bindings.Node) {
walk.Walk(&notNullMaps{}, node)
})
}

type notNullMaps struct{}

func (v *notNullMaps) Visit(node bindings.Node) walk.Visitor {
if union, ok := node.(*bindings.UnionType); ok && len(union.Types) == 2 {
hasNull := slices.ContainsFunc(union.Types, func(t bindings.ExpressionType) bool {
_, isNull := t.(*bindings.Null)
return isNull
})

var record bindings.ExpressionType
index := slices.IndexFunc(union.Types, func(t bindings.ExpressionType) bool {
ref, isRef := t.(*bindings.ReferenceType)
if !isRef {
return false
}
return ref.Name.Name == "Record"
})
if hasNull && index != -1 {
record = union.Types[index]
union.Types = []bindings.ExpressionType{record}
}
}

return v
}
3 changes: 2 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,8 @@ func (ts *Typescript) typescriptType(ty types.Type) (parsedType, error) {
return parsedType{}, xerrors.Errorf("simplify generics in map: %w", err)
}
parsed := parsedType{
Value: RecordReference(keyType.Value, valueType.Value),
// Golang `map` can be marshaled to `null` in json.
Value: bindings.Union(RecordReference(keyType.Value, valueType.Value), &bindings.Null{}),
TypeParameters: tp,
RaisedComments: append(keyType.RaisedComments, valueType.RaisedComments...),
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/anyreference/anyreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// From anyreference/anyreference.go
export interface Example {
readonly Value: Record<string, string>;
readonly Value: Record<string, string> | null;
}

// From anyreference/anyreference.go
Expand Down
6 changes: 3 additions & 3 deletions testdata/maps/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// From maps/map.go
export interface Bar<T extends any> {
readonly SimpleMap: Record<string, string>;
readonly NumberMap: Record<string, number>;
readonly GenericMap: Record<string, T>;
readonly SimpleMap: Record<string, string> | null;
readonly NumberMap: Record<string, number> | null;
readonly GenericMap: Record<string, T> | null;
}