1- import { booleanLiteral , Flow , FlowTypeAnnotation , FunctionTypeAnnotation , Identifier , identifier , isTSTypeParameter , isTypeParameter , Node , numericLiteral , stringLiteral , tSAnyKeyword , tSArrayType , tSAsExpression , tSBooleanKeyword , tSFunctionType , TSFunctionType , tSIntersectionType , tSLiteralType , tSNullKeyword , tSNumberKeyword , tSPropertySignature , tSStringKeyword , tSThisType , tSTupleType , TSType , tSTypeAnnotation , tSTypeLiteral , tSTypeParameter , tSTypeParameterDeclaration , tSTypeQuery , tSTypeReference , tSUndefinedKeyword , tSUnionType , tSVoidKeyword , TypeAnnotation , TypeParameter } from 'babel/packages/babel- types/lib '
1+ import { booleanLiteral , Flow , FlowType , FunctionTypeAnnotation , identifier , Identifier , isSpreadProperty , isTSTypeParameter , isTypeParameter , Node , numericLiteral , stringLiteral , tsAnyKeyword , tsArrayType , tsAsExpression , tsBooleanKeyword , tsFunctionType , TSFunctionType , tsIntersectionType , tsLiteralType , tsNullKeyword , tsNumberKeyword , tsPropertySignature , tsStringKeyword , tsThisType , tsTupleType , TSType , tsTypeAnnotation , tsTypeLiteral , tsTypeParameter , tsTypeParameterDeclaration , tsTypeQuery , tsTypeReference , tsUndefinedKeyword , tsUnionType , tsVoidKeyword , TypeAnnotation , TypeParameter } from '@ babel/types'
22import { generateFreeIdentifier } from './utils'
33
44// TODO: Add overloads
@@ -65,26 +65,26 @@ export function toTs(node: Flow | TSType): TSType {
6565 return toTsType ( node )
6666
6767 case 'ObjectTypeProperty' :
68- let _ = tSPropertySignature ( node . key , tSTypeAnnotation ( toTs ( node . value ) ) )
68+ let _ = tsPropertySignature ( node . key , tsTypeAnnotation ( toTs ( node . value ) ) )
6969 _ . optional = node . optional
7070 _ . readonly = node . variance && node . variance . kind === 'minus'
7171 return _
7272
7373 case 'TypeCastExpression' :
74- return tSAsExpression ( node . expression , toTs ( node . typeAnnotation ) )
74+ return tsAsExpression ( node . expression , toTs ( node . typeAnnotation ) )
7575
7676 case 'TypeParameterDeclaration' :
7777 let params = node . params . map ( _ => {
7878 let d = ( _ as any as TypeParameter ) . default
79- let p = tSTypeParameter (
79+ let p = tsTypeParameter (
8080 hasBound ( _ ) ? toTsType ( _ . bound . typeAnnotation ) : undefined ,
8181 d ? toTs ( d ) : undefined
8282 )
8383 p . name = _ . name
8484 return p
8585 } )
8686
87- return tSTypeParameterDeclaration ( params )
87+ return tsTypeParameterDeclaration ( params )
8888
8989 case 'ClassImplements' :
9090 case 'ClassProperty' :
@@ -107,44 +107,46 @@ export function toTs(node: Flow | TSType): TSType {
107107 }
108108}
109109
110- export function toTsType ( node : FlowTypeAnnotation ) : TSType {
110+ export function toTsType ( node : FlowType ) : TSType {
111111 switch ( node . type ) {
112- case 'AnyTypeAnnotation' : return tSAnyKeyword ( )
113- case 'ArrayTypeAnnotation' : return tSArrayType ( toTsType ( node . elementType ) )
114- case 'BooleanTypeAnnotation' : return tSBooleanKeyword ( )
115- case 'BooleanLiteralTypeAnnotation' : return tSLiteralType ( booleanLiteral ( node . value ) )
112+ case 'AnyTypeAnnotation' : return tsAnyKeyword ( )
113+ case 'ArrayTypeAnnotation' : return tsArrayType ( toTsType ( node . elementType ) )
114+ case 'BooleanTypeAnnotation' : return tsBooleanKeyword ( )
115+ case 'BooleanLiteralTypeAnnotation' : return tsLiteralType ( booleanLiteral ( node . value ! ) )
116116 case 'FunctionTypeAnnotation' : return functionToTsType ( node )
117- case 'GenericTypeAnnotation' : return tSTypeReference ( node . id )
118- case 'IntersectionTypeAnnotation' : return tSIntersectionType ( node . types . map ( toTsType ) )
119- case 'MixedTypeAnnotation' : return tSAnyKeyword ( )
120- case 'NullLiteralTypeAnnotation' : return tSNullKeyword ( )
121- case 'NullableTypeAnnotation' : return tSUnionType ( [ toTsType ( node . typeAnnotation ) , tSNullKeyword ( ) , tSUndefinedKeyword ( ) ] )
122- case 'NumericLiteralTypeAnnotation' : return tSLiteralType ( numericLiteral ( node . value ) )
123- case 'NumberTypeAnnotation' : return tSNumberKeyword ( )
124- case 'StringLiteralTypeAnnotation' : return tSLiteralType ( stringLiteral ( node . value ) )
125- case 'StringTypeAnnotation' : return tSStringKeyword ( )
126- case 'ThisTypeAnnotation' : return tSThisType ( )
127- case 'TupleTypeAnnotation' : return tSTupleType ( node . types . map ( toTsType ) )
128- case 'TypeofTypeAnnotation' : return tSTypeQuery ( getId ( node . argument ) )
129- case 'TypeAnnotation' : return tSTypeAnnotation ( toTsType ( node . typeAnnotation ) )
130- case 'ObjectTypeAnnotation' : return tSTypeLiteral ( [
117+ case 'GenericTypeAnnotation' : return tsTypeReference ( node . id )
118+ case 'IntersectionTypeAnnotation' : return tsIntersectionType ( node . types . map ( toTsType ) )
119+ case 'MixedTypeAnnotation' : return tsAnyKeyword ( )
120+ case 'NullLiteralTypeAnnotation' : return tsNullKeyword ( )
121+ case 'NullableTypeAnnotation' : return tsUnionType ( [ toTsType ( node . typeAnnotation ) , tsNullKeyword ( ) , tsUndefinedKeyword ( ) ] )
122+ case 'NumberLiteralTypeAnnotation' : return tsLiteralType ( numericLiteral ( node . value ! ) )
123+ case 'NumberTypeAnnotation' : return tsNumberKeyword ( )
124+ case 'StringLiteralTypeAnnotation' : return tsLiteralType ( stringLiteral ( node . value ! ) )
125+ case 'StringTypeAnnotation' : return tsStringKeyword ( )
126+ case 'ThisTypeAnnotation' : return tsThisType ( )
127+ case 'TupleTypeAnnotation' : return tsTupleType ( node . types . map ( toTsType ) )
128+ case 'TypeofTypeAnnotation' : return tsTypeQuery ( getId ( node . argument ) )
129+ case 'ObjectTypeAnnotation' : return tsTypeLiteral ( [
131130 ...node . properties . map ( _ => {
132- let s = tSPropertySignature ( _ . key , tSTypeAnnotation ( toTsType ( _ . value ) ) )
133- s . optional = _ . optional
134- return s
135- // TODO: anonymous indexers
136- // TODO: named indexers
137- // TODO: call properties
138- // TODO: variance
131+ if ( isSpreadProperty ( _ ) ) {
132+ return _
133+ }
134+ let s = tsPropertySignature ( _ . key , tsTypeAnnotation ( toTsType ( _ . value ) ) )
135+ s . optional = _ . optional
136+ return s
137+ // TODO: anonymous indexers
138+ // TODO: named indexers
139+ // TODO: call properties
140+ // TODO: variance
139141 } )
140142 // ...node.indexers.map(_ => tSIndexSignature())
141143 ] )
142- case 'UnionTypeAnnotation' : return tSUnionType ( node . types . map ( toTsType ) )
143- case 'VoidTypeAnnotation' : return tSVoidKeyword ( )
144+ case 'UnionTypeAnnotation' : return tsUnionType ( node . types . map ( toTsType ) )
145+ case 'VoidTypeAnnotation' : return tsVoidKeyword ( )
144146 }
145147}
146148
147- function getId ( node : FlowTypeAnnotation ) : Identifier {
149+ function getId ( node : FlowType ) : Identifier {
148150 switch ( node . type ) {
149151 case 'GenericTypeAnnotation' : return node . id
150152 default : throw ReferenceError ( 'typeof query must reference a node that has an id' )
@@ -156,7 +158,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
156158 let typeParams = undefined
157159
158160 if ( node . typeParameters ) {
159- typeParams = tSTypeParameterDeclaration ( node . typeParameters . params . map ( _ => {
161+ typeParams = tsTypeParameterDeclaration ( node . typeParameters . params . map ( _ => {
160162
161163 // TODO: How is this possible?
162164 if ( isTSTypeParameter ( _ ) ) {
@@ -165,13 +167,13 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
165167
166168 let constraint = _ . bound ? toTs ( _ . bound ) : undefined
167169 let default_ = _ . default ? toTs ( _ . default ) : undefined
168- let param = tSTypeParameter ( constraint , default_ )
170+ let param = tsTypeParameter ( constraint , default_ )
169171 param . name = _ . name
170172 return param
171173 } ) )
172174 }
173175
174- let f = tSFunctionType ( typeParams )
176+ let f = tsFunctionType ( typeParams )
175177
176178 // Params
177179 if ( node . params ) {
@@ -189,7 +191,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
189191 let id = identifier ( name )
190192
191193 if ( _ . typeAnnotation ) {
192- id . typeAnnotation = tSTypeAnnotation ( toTsType ( _ . typeAnnotation ) )
194+ id . typeAnnotation = tsTypeAnnotation ( toTsType ( _ . typeAnnotation ) )
193195 }
194196
195197 return id
@@ -198,7 +200,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
198200
199201 // Return type
200202 if ( node . returnType ) {
201- f . typeAnnotation = tSTypeAnnotation ( toTsType ( node . returnType ) )
203+ f . typeAnnotation = tsTypeAnnotation ( toTsType ( node . returnType ) )
202204 }
203205
204206 return f
0 commit comments