在 TypeScript 接口中描述 属性 个对象
Describe property object in TypeScript interface
我想描述一些带有嵌套对象的接口。如果不为嵌套对象创建接口怎么办?
interface ISome {
strProp:string;
complexProp:{
someStrKeyWhichIsDynamic:{
value:string;
optValue?:string;
}
};
}
我也试过了(UPD:其实没问题)
interface ISome {
strProp:string;
complexProp:{
[someStrKeyWhichIsDynamic:string]:{
value:string;
optValue?:string;
}
};
}
但是我不能像
那样分配一个对象
let dynamicStrKey = 'myKey';
{
strProp:'str',
complexProp:{
[dynamicStrKey]:{
value:'something here',
optValue: 'ok, that too',
}
};
到 ISome
类型的变量,没有类型断言 <ISome>
。至少 WebStorm 将此分配突出显示为错误。
如何正确描述嵌套对象?
你的前两个例子没有问题。他们都编译得很好,言出必行。
在您的第三个示例中,您显然希望 属性 名称为 "dynamic"。但请记住,TS 在编译时运行。在编译时,dynamicStrKey
还没有值。因此,在类型定义中尝试将其用作 属性 名称是没有意义的。您不能使用 运行 时间值定义编译时工件。
第二部分的代码支持动态 properties.you 不能与最后一个一起使用,因为类型不会发送到 javascript code.I 认为你只是喜欢下面的东西,使用泛型 instead.for 更多细节,你可以看 typescript index types.
interface ISome<K extends string> {
strProp: string;
complexProp: {
[P in K]: {
value: string;
optValue?: string;
}
};
}
let foo: ISome<"foo"> = {
strProp:"foo",
complexProp:{
foo:{
value:"foo"
}
}
};
let bar: ISome<"bar"> = {
strProp:"bar",
complexProp:{
bar:{
value:"bar",
optValue:"<optional>"
}
}
};
let foobar: ISome<"foo"|"bar"> = {
strProp:"foo",
complexProp:{
foo:{
value:"foo"
},
bar:{
value:"bar",
optValue:"<optional>"
}
}
};
// interesting things that use with any|never types
let anything:ISome<any|never>={
strProp:"foo",
complexProp:{}
};
最后,我认为我的第二个变体是正确的
interface ISome {
strProp:string;
complexProp:{
[someStrKeyWhichIsDynamic:string]:{
value:string;
optValue?:string;
}
};
}
对于动态键,你可以只写[dynamic:string]
来指定这里将是一些字符串属性。好像我遇到了与问题无关的 webstorm 错误。
顺便说一句,如果你有一些基于字符串的枚举,你可能想使用 [key in MyEnum]: {...}
而不是 [key:string]
。解决错误:
TS1337 an index signature parameter type can not be a union type.
如果你有一个文字对象,例如
const obj = { prop1: 'blah', prop2: 'blahblah' }
您可能想使用 [key in keyof typeof obj]: {...}
来描述您的动态密钥只能是 'prop1' 或 'prop2'(或者,更通用,来自 Object.keys(obj
的值))
我想描述一些带有嵌套对象的接口。如果不为嵌套对象创建接口怎么办?
interface ISome {
strProp:string;
complexProp:{
someStrKeyWhichIsDynamic:{
value:string;
optValue?:string;
}
};
}
我也试过了(UPD:其实没问题)
interface ISome {
strProp:string;
complexProp:{
[someStrKeyWhichIsDynamic:string]:{
value:string;
optValue?:string;
}
};
}
但是我不能像
那样分配一个对象let dynamicStrKey = 'myKey';
{
strProp:'str',
complexProp:{
[dynamicStrKey]:{
value:'something here',
optValue: 'ok, that too',
}
};
到 ISome
类型的变量,没有类型断言 <ISome>
。至少 WebStorm 将此分配突出显示为错误。
如何正确描述嵌套对象?
你的前两个例子没有问题。他们都编译得很好,言出必行。
在您的第三个示例中,您显然希望 属性 名称为 "dynamic"。但请记住,TS 在编译时运行。在编译时,dynamicStrKey
还没有值。因此,在类型定义中尝试将其用作 属性 名称是没有意义的。您不能使用 运行 时间值定义编译时工件。
第二部分的代码支持动态 properties.you 不能与最后一个一起使用,因为类型不会发送到 javascript code.I 认为你只是喜欢下面的东西,使用泛型 instead.for 更多细节,你可以看 typescript index types.
interface ISome<K extends string> {
strProp: string;
complexProp: {
[P in K]: {
value: string;
optValue?: string;
}
};
}
let foo: ISome<"foo"> = {
strProp:"foo",
complexProp:{
foo:{
value:"foo"
}
}
};
let bar: ISome<"bar"> = {
strProp:"bar",
complexProp:{
bar:{
value:"bar",
optValue:"<optional>"
}
}
};
let foobar: ISome<"foo"|"bar"> = {
strProp:"foo",
complexProp:{
foo:{
value:"foo"
},
bar:{
value:"bar",
optValue:"<optional>"
}
}
};
// interesting things that use with any|never types
let anything:ISome<any|never>={
strProp:"foo",
complexProp:{}
};
最后,我认为我的第二个变体是正确的
interface ISome {
strProp:string;
complexProp:{
[someStrKeyWhichIsDynamic:string]:{
value:string;
optValue?:string;
}
};
}
对于动态键,你可以只写[dynamic:string]
来指定这里将是一些字符串属性。好像我遇到了与问题无关的 webstorm 错误。
顺便说一句,如果你有一些基于字符串的枚举,你可能想使用 [key in MyEnum]: {...}
而不是 [key:string]
。解决错误:
TS1337 an index signature parameter type can not be a union type.
如果你有一个文字对象,例如
const obj = { prop1: 'blah', prop2: 'blahblah' }
您可能想使用 [key in keyof typeof obj]: {...}
来描述您的动态密钥只能是 'prop1' 或 'prop2'(或者,更通用,来自 Object.keys(obj
的值))