Typescript 的 Record<string, any> 的等效 DataWeave 类型是什么?
What is the equivalent DataWeave type for Typescript's Record<string, any>?
DataWeave has a type declaration system similar to Typescript.
我想定义一个相当于 Typescript 的 DataWeave 类型 Record<string, V>
。
具体来说,我想定义一个类似映射的类型,其中我不知道具体的键,但我知道它们是字符串,而且我知道值是什么。
例如:
{
"foo": { "answer": 42 },
"bar": { "answer": 1337 },
"this key could be any string": { "answer": 1 }
}
我试过的
根据 DataWeave's v2.3 documentation,我可以将 object
类型声明为:
type User = {
firstName: String,
lastName: String,
age: Number
}
但是,假设我知道该对象中的键是什么,即 firstName
、lastName
和 age
。在我的用例中,我不知道键是什么,只知道它们是字符串。
谢谢
根据 DataWeave 中 Objects 的文档,键的类型为 Key
,不能是任意类型。一个Key包含一个名字和属性,名字永远是一个String
。我知道这意味着默认情况下对象基本上等同于您想要实现的目标。实际上,似乎无法使用不同类型的键来定义对象。
Object (dw::Core Type)
Represents any object as a collection of Key:value pairs, where a Key
is composed of a Name and Attributes.
The Name type is composed of a String as the local name and the
Namespace. Attributes is composed of an Array of Name:value pairs.
Note that the Key is not a String, so it is not possible to compare
keys. However, you can get the local name by performing an as String
type coercion on any value of type Key.
在 src/main/resources/weave/autogenerated
查看我的 Mule4 项目中的自动生成类型后,我发现未知的关键属性由 _
(下划线)表示。
示例:
type Foo = {
_: {
name: String
}
}
var myfoo: Foo = {
"a": {
name: "aa"
},
"b": {
name: "bb"
}
}
DataWeave 现在将在您输入 myfoo['any string here'].
时自动建议 name
属性
DataWeave has a type declaration system similar to Typescript.
我想定义一个相当于 Typescript 的 DataWeave 类型 Record<string, V>
。
具体来说,我想定义一个类似映射的类型,其中我不知道具体的键,但我知道它们是字符串,而且我知道值是什么。
例如:
{
"foo": { "answer": 42 },
"bar": { "answer": 1337 },
"this key could be any string": { "answer": 1 }
}
我试过的
根据 DataWeave's v2.3 documentation,我可以将 object
类型声明为:
type User = {
firstName: String,
lastName: String,
age: Number
}
但是,假设我知道该对象中的键是什么,即 firstName
、lastName
和 age
。在我的用例中,我不知道键是什么,只知道它们是字符串。
谢谢
根据 DataWeave 中 Objects 的文档,键的类型为 Key
,不能是任意类型。一个Key包含一个名字和属性,名字永远是一个String
。我知道这意味着默认情况下对象基本上等同于您想要实现的目标。实际上,似乎无法使用不同类型的键来定义对象。
Object (dw::Core Type)
Represents any object as a collection of Key:value pairs, where a Key is composed of a Name and Attributes.
The Name type is composed of a String as the local name and the Namespace. Attributes is composed of an Array of Name:value pairs. Note that the Key is not a String, so it is not possible to compare keys. However, you can get the local name by performing an as String type coercion on any value of type Key.
在 src/main/resources/weave/autogenerated
查看我的 Mule4 项目中的自动生成类型后,我发现未知的关键属性由 _
(下划线)表示。
示例:
type Foo = {
_: {
name: String
}
}
var myfoo: Foo = {
"a": {
name: "aa"
},
"b": {
name: "bb"
}
}
DataWeave 现在将在您输入 myfoo['any string here'].
name
属性