DataWeave has a type declaration system similar to Typescript.
I want to define a DataWeave type that is equivalent to Typescript's Record<string, V>
.
Specifically, I want to define a map-like type wherein I do not know the specific keys but I do know that they are Strings, and I do know what the values will be.
For example:
{
"foo": { "answer": 42 },
"bar": { "answer": 1337 },
"this key could be any string": { "answer": 1 }
}
Per DataWeave's v2.3 documentation, I can declare an object
type as:
type User = {
firstName: String,
lastName: String,
age: Number
}
However, that assumes I know what the keys in that object are, namely firstName
, lastName
, and age
. In my use case, I do not know what the keys are, only that they are strings.
Thanks
After reviewing the autogenerated types in my Mule4 project at src/main/resources/weave/autogenerated
, I discovered that unknown key properties are represented by _
(underscore).
Example:
type Foo = {
_: {
name: String
}
}
var myfoo: Foo = {
"a": {
name: "aa"
},
"b": {
name: "bb"
}
}
DataWeave will now autosuggest name
property when you write myfoo['any string here'].