MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / TypeToTSType

Function TypeToTSType

pkg/tsgen/tsgen.go:127–175  ·  view source on GitHub ↗
(t reflect.Type, tsTypesMap map[reflect.Type]string)

Source from the content-addressed store, hash-verified

125}
126
127func TypeToTSType(t reflect.Type, tsTypesMap map[reflect.Type]string) (string, []reflect.Type) {
128 switch t.Kind() {
129 case reflect.String:
130 return "string", nil
131 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
132 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
133 reflect.Float32, reflect.Float64:
134 return "number", nil
135 case reflect.Bool:
136 return "boolean", nil
137 case reflect.Slice, reflect.Array:
138 // special case for byte slice, marshals to base64 encoded string
139 if t.Elem().Kind() == reflect.Uint8 {
140 return "string", nil
141 }
142 elemType, subTypes := TypeToTSType(t.Elem(), tsTypesMap)
143 if elemType == "" {
144 return "", nil
145 }
146 return fmt.Sprintf("%s[]", elemType), subTypes
147 case reflect.Map:
148 if t.Key().Kind() != reflect.String {
149 return "", nil
150 }
151 if t == metaRType {
152 return "MetaType", nil
153 }
154 elemType, subTypes := TypeToTSType(t.Elem(), tsTypesMap)
155 if elemType == "" {
156 return "", nil
157 }
158 return fmt.Sprintf("{[key: string]: %s}", elemType), subTypes
159 case reflect.Struct:
160 name := t.Name()
161 if tsRename := tsRenameMap[name]; tsRename != "" {
162 name = tsRename
163 }
164 return name, []reflect.Type{t}
165 case reflect.Ptr:
166 return TypeToTSType(t.Elem(), tsTypesMap)
167 case reflect.Interface:
168 if _, ok := tsTypesMap[t]; ok {
169 return t.Name(), nil
170 }
171 return "any", nil
172 default:
173 return "", nil
174 }
175}
176
177var tsRenameMap = map[string]string{
178 "Window": "WaveWindow",

Calls 2

KeyMethod · 0.80
NameMethod · 0.45

Tested by

no test coverage detected