【发布时间】:2021-08-15 12:52:15
【问题描述】:
我正在开发的模块 ( module1 ) 中有以下接口类型。
interface ModuleOneInterface {
keyOne: customInterface;
keyTwo: customInterface;
keyThree: customInterface;
}
所以我目前正在开发第二个模块 (module2),它使用上述接口作为其组件中的道具。上面的接口不是从模块( module1 )导出的,因此我无法导入它( module2 )。我声明了另一个接口,它可以代表我的第二个模块( module2 )中的类型。下面是界面。
export interface ModuleTwoInterface {
//This is the one which is accepting module one interface
module2KeyOne: Record<string, customInterface>;
module2KeyTwo: string;
}
但这会引发以下错误。
TS2345: Argument of type 'ModuleOneInterface' is not assignable to parameter
of type 'Record<string, customInterface>'.
我在这里做错了什么?我也尝试了以下解决方案。但效果并不好。
interface moduleTwoSubInterface {
[ key: string ]: customInterface;
}
export interface ModuleTwoInterface {
// This is the one which is accepting module one interface
module2KeyOne: moduleTwoSubInterface;
module2KeyTwo: string;
}
任何帮助将不胜感激。
【问题讨论】:
-
如果
ModuleOneInterface已经被另一个外部模块使用,这似乎是一个很好的导出它的动力,不是吗?包很少会省略类型,通常甚至是私有/内部类型,因为类型系统的重点是让您的模块更容易开发。
标签: typescript typescript-typings react-typescript