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

Emit Fable erased unions as TS union types #3396

Merged
merged 9 commits into from
Apr 4, 2023
Merged

Conversation

alfonsogarciacaro
Copy link
Member

@alfonsogarciacaro alfonsogarciacaro commented Mar 31, 2023

This PR stops compiling erased types as Any so they can be dealt with in the Fable2XXX step. @ncave @dbrattli Not sure how it affects Rust and Python, although tests are passing. For JS/TS I'm skipping the declaration and for TS I'm transforming the annotations to TS unions/tuples when possible:

| _, Patterns.Try (Util.tryFindAnyAttribute [Atts.erase; Atts.tsTaggedUnion]) (erasedAtt: Fable.Attribute) ->
if erasedAtt.Entity.FullName = Atts.erase && ent.IsFSharpUnion then
let genArgs =
List.zip ent.GenericParameters genArgs
|> List.map (fun (p, a) -> p.Name, a)
|> Map
let transformSingleFieldType (uci: Fable.UnionCase) =
List.tryHead uci.UnionCaseFields
|> Option.map (fun fi -> fi.FieldType |> resolveInlineType genArgs |> makeTypeAnnotation com ctx)
|> Option.defaultValue VoidTypeAnnotation
match ent.UnionCases with
| [uci] when List.isMultiple uci.UnionCaseFields ->
uci.UnionCaseFields
|> List.mapToArray (fun fi -> fi.FieldType |> resolveInlineType genArgs |> makeTypeAnnotation com ctx)
|> TupleTypeAnnotation
| [uci] -> transformSingleFieldType uci
| ucis -> ucis |> List.mapToArray transformSingleFieldType |> UnionTypeAnnotation
// TODO: tsTaggedUnion
else AnyTypeAnnotation

@Booksbaum An unfortunate consequence is this breaks some of the integration tests for anonymous records casted to interfaces because U2, U3... are not compiled as Any anymore. For now I just commented out the test because I think it's not a common pattern to have an anonymous record with a field like U2<string, int> (you either have string or int) but we can try to bring them back if needed. I also moved the code to deal with this from FSharp2Fable.Util to Replacements.Util, as it's only use from JS and Python replacements.

Also, at the end I added a commit to optimize compile-time equality to reduce generated code for patterns like this (see #3394).

@alfonsogarciacaro alfonsogarciacaro merged commit 216d3d1 into main Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants