1

I'm writting a function transpiled with Scala.js that should accept any random JavaScript object.

Ex:

// This has been transpiled using Scala.js
var my_service = new com.myself.Service();

// This is plain JavaScript 
var result1 = service({ hello: "yolo" });
var result2 = service({ whatever: "ok", really: { yes: "right", no: "don't" } });

However, I can't find the input type that matches it.

  • What would be the Scala-function signature to get such a thing?
  • Could it be used then as a "Scala/JS case class" easily?

Note (if it helps giving a direction for the answers): these objects have, in the real life, an expected schema but it cannot be created as JS object generated from Scala.js since it comes from another consumed service.

3
  • 1
    Those are JavaScript object literals (containing strings and more object literals). They aren't JSON. If you extracted them from the JavaScript and put them in a file of their own then they still wouldn't be JSON as JSON only allows strings as property names (and not identifiers as you are using here). Commented Aug 11, 2016 at 9:01
  • Alright. I'll rephrase it. Thanks. Commented Aug 11, 2016 at 9:04
  • 1
    "Object" would be sufficient Commented Aug 11, 2016 at 9:04

1 Answer 1

1

Since the objects have an expected schema, its probably easiest to define a facade type:

@js.native
trait Args extends js.Object {
  val hello: js.UndefOr[String]
  val whatever: js.UndefOr[String]
  val really: js.UndefOr[ReallyArgs]
}

@js.native
trait Really extends js.Object {
  val yes: String
  val no: String
}

def myMethod(args: Args): Unit = {
  println(args.hello)
  println(args.really.map(_.yes))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.