【发布时间】:2020-02-04 17:10:21
【问题描述】:
我定义的对象:
export const exceptionMsg: ErrorMessages = {
badRequest: 'bla bla bla',
user: {
notFound: 'The user bla bla bla',
},
};
我为它定义的接口ErrorMessages:
interface ErrorMessages {
[x: string]: string | { [x: string]: string };
}
基本上,我的对象的属性可以是字符串,也可以是其值也是字符串的对象。这就是我要定义的。但是,在另一个文件中使用此对象时,会引发此错误:
“字符串 | 类型”上不存在属性“未找到” { [x:字符串]:字符串; }'。 类型“字符串”上不存在属性“notFound”。
我使用它的代码是这样的:
throw new BadRequestException(exceptionMsg.pipeline.notFound);
我对 Typescript 比较陌生,但在阅读文档后,我似乎找不到我缺少的东西。
【问题讨论】:
-
您的意思是当您使用
exceptionMsg.notFound时会引发错误? -
@TitianCernicova-Dragomir 是的,让我编辑一下
标签: typescript typescript-typings