Skip to content

Commit 0ada0ce

Browse files
committed
feat: add support for optional type
[converter] Fix #68
1 parent 2000f26 commit 0ada0ce

File tree

7 files changed

+45
-4
lines changed

7 files changed

+45
-4
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
187187
SettingsContainer.#privateField = $0"""
188188
```
189189

190+
* Add support for optional type
191+
192+
```ts
193+
export type LatLngTuple = [number, number, number?];
194+
```
195+
196+
```fs
197+
type LatLngTuple = float * float * float option
198+
```
199+
190200
### Changed
191201

192202
* Replace `Boolean` with `bool`

src/Glutinum.Converter/GlueAST.fs

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ type GlueType =
209209
| TupleType of GlueType list
210210
| IntersectionType of GlueMember list
211211
| TypeLiteral of GlueTypeLiteral
212+
| OptionalType of GlueType
212213

213214
member this.Name =
214215
match this with
@@ -243,4 +244,5 @@ type GlueType =
243244
| Partial _
244245
| FunctionType _
245246
| TupleType _
247+
| OptionalType _ // TODO: Should we take the name of the underlying type and add option to it?
246248
| Discard -> "obj"

src/Glutinum.Converter/Reader/TypeNode.fs

+5
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ let readTypeNode
309309

310310
reader.ReadTypeNode parenthesizedTypeNode.``type``
311311

312+
| Ts.SyntaxKind.OptionalType ->
313+
let optionalTypeNode = typeNode :?> Ts.OptionalTypeNode
314+
315+
reader.ReadTypeNode optionalTypeNode.``type`` |> GlueType.OptionalType
316+
312317
| _ ->
313318
generateReaderError
314319
"type node"

src/Glutinum.Converter/Transform.fs

+9-4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ let rec private transformType
126126
| GlueType.Primitive primitiveInfo ->
127127
transformPrimitive primitiveInfo |> FSharpType.Primitive
128128

129+
| GlueType.OptionalType glueType ->
130+
transformType context glueType |> FSharpType.Option
131+
129132
| GlueType.ThisType typeName -> FSharpType.ThisType typeName
130133

131134
| GlueType.TupleType glueTypes -> transformTupleType context glueTypes
@@ -178,7 +181,7 @@ let rec private transformType
178181
|> FSharpType.TypeReference
179182

180183
| GlueType.Array glueType ->
181-
(transformType context) glueType |> FSharpType.ResizeArray
184+
transformType context glueType |> FSharpType.ResizeArray
182185

183186
| GlueType.ClassDeclaration classDeclaration ->
184187
({
@@ -198,7 +201,7 @@ let rec private transformType
198201
functionTypeInfo.Parameters
199202
|> List.map (transformParameter context)
200203
TypeArguments = []
201-
ReturnType = (transformType context) functionTypeInfo.Type
204+
ReturnType = transformType context functionTypeInfo.Type
202205
}
203206
: FSharpFunctionType)
204207
|> FSharpType.Function
@@ -328,7 +331,7 @@ let private transformExports
328331
info.Parameters |> List.map (transformParameter context)
329332
TypeParameters =
330333
transformTypeParameters context info.TypeParameters
331-
Type = (transformType context) info.Type
334+
Type = transformType context info.Type
332335
IsOptional = false
333336
IsStatic = true
334337
Accessor = None
@@ -1208,7 +1211,8 @@ let private transformTypeAliasDeclaration
12081211
| GlueType.Discard
12091212
| GlueType.FunctionDeclaration _
12101213
| GlueType.ThisType _
1211-
| GlueType.Variable _ -> FSharpType.Discard
1214+
| GlueType.Variable _
1215+
| GlueType.OptionalType _ -> FSharpType.Discard
12121216

12131217
let private transformModuleDeclaration
12141218
(moduleDeclaration: GlueModuleDeclaration)
@@ -1295,6 +1299,7 @@ let rec private transformToFsharp
12951299
| GlueType.TupleType _
12961300
| GlueType.IntersectionType _
12971301
| GlueType.TypeLiteral _
1302+
| GlueType.OptionalType _
12981303
| GlueType.ThisType _ -> FSharpType.Discard
12991304
)
13001305

src/Glutinum.Web/Pages/Editors.GlueAST.GlueASTViewer.fs

+6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ type GlueASTViewer =
164164
]
165165
context
166166

167+
| GlueType.OptionalType optionalType ->
168+
ASTViewer.renderNode
169+
"OptionalType"
170+
[ GlueASTViewer.Type optionalType ]
171+
context
172+
167173
| GlueType.Interface interfaceInfo ->
168174
ASTViewer.renderNode
169175
"Interface"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type LatLngTuple = [number, number, number?];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module rec Glutinum
2+
3+
open Fable.Core
4+
open Fable.Core.JsInterop
5+
open System
6+
7+
type LatLngTuple =
8+
float * float * float option
9+
10+
(***)
11+
#r "nuget: Fable.Core"
12+
(***)

0 commit comments

Comments
 (0)