Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS] Make discriminated union .Is* properties works #3982

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [Python] Fix type testing against `uint8`, `uint32`, `uint64`, `decimal` (@MangelMaxime)
* [JS/TS] Workaround source map generation bug (deteriorate them a little) (@MangelMaxime)
* [TS] Make discriminated union `.Is*` properties works (@MangelMaxime)

## 5.0.0-alpha.2 - 2024-11-25

Expand Down
20 changes: 18 additions & 2 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,13 @@ module Util =
else
expr

getExpr range (getExpr None expr (Expression.stringLiteral ("fields"))) (ofInt info.FieldIndex)
let expr =
getExpr range (getExpr None expr (Expression.stringLiteral ("fields"))) (ofInt info.FieldIndex)

if com.IsTypeScript then
AsExpression(expr, makeTypeAnnotation com ctx Fable.Type.Any)
else
expr

let transformSet (com: IBabelCompiler) ctx range fableExpr typ (value: Fable.Expr) kind =
let expr = com.TransformAsExpr(ctx, fableExpr)
Expand Down Expand Up @@ -2438,7 +2444,17 @@ module Util =
expr
| Fable.UnionCaseTest tag ->
let expected = transformUnionCaseTag com range expr.Type tag
let actual = getUnionExprTag com ctx None expr

let unionExprTag = getUnionExprTag com ctx None expr

let actual =
if com.IsTypeScript then
// In TypeScript, we need to cast the union case tag to a number
// otherwise TypeScript compiler is too smart and test against the literal value (0, 1, 2, etc.)
let number = Fable.Number(Int32, Fable.NumberInfo.Empty)
(AsExpression(unionExprTag, makeTypeAnnotation com ctx number))
else
unionExprTag

Expression.binaryExpression (BinaryEqual, actual, expected, ?loc = range)

Expand Down
Loading