【发布时间】:2021-02-24 22:31:44
【问题描述】:
对于两种对象类型的深度/递归交集,是否有类似“&”的东西?这意味着只允许访问所有合并类型中存在的属性。
这是我想做的:
// Defined in its own file
const en = {
"welcome": {
"hello": "Hello",
"world": "World"
}
}
// Defined in its own file
const hu = {
"welcome": {
"hello": "Helló világ"
}
}
// How to write this type?
// Should be deep intersection,
// meaning only mutually available properties allowed
let locale: typeof en & typeof hu
// Using a Webpack resolve.alias
// resolve.alias.locales = path.resolve(
// __dirname,
// `locales/${process.env.NEXT_PUBLIC_LOCALE}`
// )
//@ts-ignore
locale = {}
locale.welcome.world // This should NOT be available
locale.welcome.hello // This SHOULD be availeble
【问题讨论】:
-
您说的是联合 (
|),而不是交集,它的行为已经如此。
标签: typescript typescript-typings intersection recursive-type