i am trying to access a property which is available in deep inside of an object. How to do this in scala js type safe way?
internetMessageId property is available on Office object which is accessible in plain javascript in below way :
Office.context.mailbox.item.internetMessageId
My scala JS facade of Office object :
@js.native
@JSGlobal
object Office extends js.Object {
var initialize:js.Function1[String, _] = js.native
}
How to write access method for property internetMessageId on Office object in ScalaJs?
Well,
internetMessageIdis not a property ofOffice.Officeneeds to have propertycotnext, for which you define a@js.native trait Contextfacade, which should have a propertymailbox, for which you define a@js.native trait Contextfacade, and so on until you get to@js.native trait Itemwhich finally defines a propertyinternetMessageId.Example trait code for this hierarchy:
Then
Office.context.mailbox.item.internetMessageIdwill actually be valid, type-checked Scala code.However, remember that when defining such
@js.nativefacades the Scala compiler assumes that you have defined them correctly. So if you have e.g. misspelledcontextlike I did in the first paragraph, your code will fail at runtime. So your type safety is as good as your facades are.So, if you only need to access this one field out of the whole nested structure, you can save time by just defining some
getInternetMessageIdmethod somewhere, which would access the deeply nested field in a non-typesafe way, usingjs.Dynamic. It would look kinda weird and ad-hoc, but that's because it is. But it would not be less type safe.My point is, that you will have to contain "type-unsafety" somewhere when dealing with JS-provided objects. It's best to contain it within the facades (option 1), but sometimes that's too much overhead.