Skip to content

Commit cffad1b

Browse files
committed
feat: handle unnamed union type constraints
1 parent 0621455 commit cffad1b

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

convert.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,20 @@ func (ts *Typescript) typescriptType(ty types.Type) (parsedType, error) {
997997
case *types.Alias:
998998
// TODO: Verify this is correct.
999999
return ts.typescriptType(ty.Underlying())
1000+
case *types.Union:
1001+
allTypes := make([]bindings.ExpressionType, 0, ty.Len())
1002+
for i := 0; i < ty.Len(); i++ {
1003+
constraintType, err := ts.typescriptType(ty.Term(i).Type())
1004+
if err != nil {
1005+
return parsedType{}, xerrors.Errorf("union %q: %w", ty.String(), err)
1006+
}
1007+
allTypes = append(allTypes, constraintType.Value)
1008+
}
1009+
return parsedType{
1010+
Value: bindings.Union(allTypes...),
1011+
TypeParameters: nil,
1012+
RaisedComments: nil,
1013+
}, nil
10001014
}
10011015

10021016
// These are all the other types we need to support.

testdata/union/union.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package union
2+
3+
type UnionConstraint[T string | int64] struct {
4+
Value T
5+
}

testdata/union/union.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Code generated by 'guts'. DO NOT EDIT.
2+
3+
// From union/union.go
4+
export interface UnionConstraint<T extends string | number> {
5+
readonly Value: T;
6+
}

0 commit comments

Comments
 (0)