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

Cannot infer key and value of [object Object] #1290

Closed
nozzlegear opened this issue Dec 8, 2017 · 3 comments
Closed

Cannot infer key and value of [object Object] #1290

nozzlegear opened this issue Dec 8, 2017 · 3 comments

Comments

@nozzlegear
Copy link

Description

Hello, I'm having some problems writing bindings for a small React component. I have a typical union type with cases that represent props for the component. One of those cases looks like Tabs of ITab list. When I try to turn a list of these props into an object with keyValueList it works fine, until I add the Tabs prop, where the JavaScript will then throw an error saying Cannot infer key and value of [object Object].

Repro code

open Fable.Core
open Fable.Core.JsInterop

type ITab = 
  { Name: string
    Selected: bool }

type Props = 
    | Tabs of ITab list
    | Title of string
  
let tab = { Name = "Hello"; Selected = true }
let works = keyValueList CaseRules.LowerFirst [Title "My Title"]
let doesNotWork = keyValueList CaseRules.None [Tabs [tab]; Title "My Title"]

Expected and actual results

I'd expect the doesNotWork value to not throw an error and generate an object that looks like:

var doesNotWork = {
  title: "My Title",
  tabs: [
    { 
      name: "Hello",
      selected: true
    }
  ]
}

Related information

  • Fable version (dotnet fable --version): 1.3.0
  • Operating system: Windows 10 Pro
@MangelMaxime
Copy link
Member

You cna fix your problem by using an array:

open Fable.Core
open Fable.Core.JsInterop

type ITab = 
  { Name: string
    Selected: bool }

type Props = 
    | Tabs of ITab array
    | Title of string
  
let tab = { Name = "Hello"; Selected = true }
let works = keyValueList CaseRules.LowerFirst [Title "My Title"]
let doesNotWork = keyValueList CaseRules.None [Tabs [| tab |]; Title "My Title"]

Problem occur because Json/JObject don't use list type which is an object on runtime. Json/Jobject only know arrays.

@nozzlegear
Copy link
Author

Thank you! I wasn't aware of that limitation, everything's working now after switching over to array.

@alfonsogarciacaro
Copy link
Member

Yes, actually if you use a list keyValueList will interpret that's a nested keyValueList, as in the case of Style:

div [ClassName "foo"
     Style [BackgroundColor "red"
            Width "500px"]] []

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

3 participants