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

Missing discriminator field when using TypeScriptTaggedUnion #3837

Closed
joprice opened this issue Jun 10, 2024 · 1 comment
Closed

Missing discriminator field when using TypeScriptTaggedUnion #3837

joprice opened this issue Jun 10, 2024 · 1 comment

Comments

@joprice
Copy link
Contributor

joprice commented Jun 10, 2024

Description

When creating an enum value from a TypeScriptTaggedUnion, the type field doesn't get added if a case has a single argument.

Repro code

open Fable.Core

[<TypeScriptTaggedUnion("type")>]
type AuthState =
| Zero
| One of string
| Two of string * string

let zero = AuthState.Zero
let one = AuthState.One "a"
let two = AuthState.Two ("a", "b")

let check = function 
  | Zero -> "zero"
  | One _ -> "one"
  | Two _ -> "two"

https://fable.io/repl/#?code=PYBwpgdgBAYghgIwDZgHQGFgCcwFgBQBA2gDwAqAnuAMoDGWAliAC5lwDm7YAJgKoQNgEABQAiZlTCiAlAD4AugQngoAQQCuzABbVmcZmCgBeAgB8oALTBZgZqAHkIh4ADMoAZ2aMI7O2QDuwFCuHl4MPlAAVKHevoT4KMxQAF7WQUZqmjp6BqhWNgSJwU7Gmdq6+miOhqJwooVgScyBpRrlOWgBQWJ1ADRQoggyBA1JtFpgtADWpS7qELTMgtAEUFDm+UEAtLIDqTb1+Gvm1VAA+lA7A0JSq+tQXeeXu+KBokA&html=Q&css=Q

Expected and actual results

The One case gets compiled to the raw value, without the discriminator field "type", while the pattern match is checking for a field that will never be present:

export const one = "a";
....
export function check(_arg) {
    switch (_arg.type) {
        case "one":
....

I would expect it to look similar to Two:

export const one = {
    type: "one",
    Item1: "a"
};

Related information

  • Fable version: 4.18.0
@joprice
Copy link
Contributor Author

joprice commented Jun 10, 2024

Just checked some other examples and found that adding a label to the One case definition (| One of value:string) fixes the generated code.

| [ argExpr ] when not (FsUnionCase.HasNamedFields unionCase) -> argExpr
| _ ->
let isCompiledValue, tagExpr =
match FsUnionCase.CompiledValue unionCase with
| None -> false, transformStringEnum rule unionCase
| Some(CompiledValue.Integer i) -> false, makeIntConst i
| Some(CompiledValue.Float f) -> false, makeFloatConst f
| Some(CompiledValue.Boolean b) -> false, makeBoolConst b

I also notice now that the labels on Two of Item1 and Item2 are the field names of the tuple. Perhaps this attribute should enforce labels when passed at least single arguments and maybe tuples as well?

ncave added a commit to ncave/Fable that referenced this issue Jun 13, 2024
@ncave ncave mentioned this issue Jun 13, 2024
ncave added a commit to ncave/Fable that referenced this issue Jun 13, 2024
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

No branches or pull requests

1 participant