Skip to content

Commit b0d5656

Browse files
authored
[TS] Correctly resolve type references for TypeScriptTaggedUnion (#4380)
1 parent ac432a7 commit b0d5656

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [TS] Correctly resolve type references for `TypeScriptTaggedUnion` (by @MangelMaxime and @jrwone0)
1213
* [TS] Expose optional `stack` property on `Exception` (by @MangelMaxime)
1314
* [Python] Fix `nonlocal`/`global` declarations generated inside `match/case` bodies causing `SyntaxError` (by @dbrattli)
1415
* [Python] Fix exception variable captured in deferred closures causing `NameError` (PEP 3110 scoping) (by @dbrattli)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [TS] Correctly resolve type references for `TypeScriptTaggedUnion` (by @MangelMaxime and @jrwone0)
1213
* [Python] Fix `nonlocal`/`global` declarations generated inside `match/case` bodies causing `SyntaxError` (by @dbrattli)
1314
* [Python] Fix exception variable captured in deferred closures causing `NameError` (PEP 3110 scoping) (by @dbrattli)
1415

src/Fable.Transforms/FSharp2Fable.fs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,16 @@ let private transformNewUnion com ctx r fsType (unionCase: FSharpUnionCase) (arg
9696

9797
let fieldTypes = makeTypeGenArgs ctx.GenericArgs fieldTypes
9898

99-
Fable.NewAnonymousRecord(
100-
tagExpr :: argExprs,
101-
Array.append [| tagName |] fieldNames,
102-
tagExpr.Type :: fieldTypes,
103-
false
104-
)
105-
|> makeValue r
99+
let expr =
100+
Fable.NewAnonymousRecord(
101+
tagExpr :: argExprs,
102+
Array.append [| tagName |] fieldNames,
103+
tagExpr.Type :: fieldTypes,
104+
false
105+
)
106+
|> makeValue r
107+
108+
Fable.TypeCast(expr, makeType ctx.GenericArgs fsType)
106109

107110
| StringEnum(tdef, rule) ->
108111
match argExprs with

tests/Js/Main/JsInteropTests.fs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ module TaggedUnion =
339339
| [<CompiledValue(Kind.Bar)>] Bar of Bar<Kind>
340340
| [<CompiledValue(Kind.Baz)>] Baz of Baz<Kind>
341341

342+
343+
[<RequireQualifiedAccess>]
344+
[<TypeScriptTaggedUnion("type")>]
345+
type SimpleFoo =
346+
| Bar of baz: int
347+
348+
[<RequireQualifiedAccess>]
349+
[<TypeScriptTaggedUnion("type")>]
350+
type FooGeneric<'a> =
351+
| Bar of baz: 'a
352+
342353
#if FABLE_COMPILER
343354
module PojoDefinedByConsArgs =
344355
[<JS.Pojo; AllowNullLiteral>]
@@ -960,6 +971,28 @@ let tests =
960971
TaggedUnion.EnumTagged.Foo !!{| kind = TaggedUnion.Kind.Foo; foo = "hello" |} |> describe |> equal "foo: hello"
961972
TaggedUnion.EnumTagged.Bar !!{| kind = TaggedUnion.Kind.Bar; bar = 42 |} |> describe |> equal "bar: 42"
962973
TaggedUnion.EnumTagged.Baz !!{| kind = TaggedUnion.Kind.Baz; baz = false |} |> describe |> equal "baz: false"
974+
975+
// Fix https://github.com/fable-compiler/Fable/issues/4378
976+
testCase "TypeScriptTaggedUnion produce the correct return type for a function" <| fun () ->
977+
let getFoo (baz: int) =
978+
(TaggedUnion.SimpleFoo.Bar baz)
979+
980+
let foo = getFoo 10
981+
982+
equal (TaggedUnion.SimpleFoo.Bar 10) foo
983+
984+
testCase "TypeScriptTaggedUnion produce the correct type for a value" <| fun () ->
985+
let foo1 = TaggedUnion.SimpleFoo.Bar 10
986+
987+
equal (TaggedUnion.SimpleFoo.Bar 10) foo1
988+
989+
testCase "TypeScriptTaggedUnion produce the type even with generic type parameters" <| fun () ->
990+
let getFooGeneric (baz: 'a) : TaggedUnion.FooGeneric<'a> =
991+
(TaggedUnion.FooGeneric.Bar baz)
992+
993+
let fooGeneric = getFooGeneric 10
994+
995+
equal (TaggedUnion.FooGeneric.Bar 10) fooGeneric
963996
#endif
964997

965998
testCase "Pattern matching with StringEnum works" <| fun () ->

0 commit comments

Comments
 (0)