如何将对象的 $Shape 传播到精确对象中
How do I spread the $Shape of an object into an Exact object
我正在尝试将第三方库提供的对象定义作为我的确切对象类型的可选属性进行传播。
示例:
type Match = {
params: { [key: string]: ?string, ... },
isExact: boolean,
path: string,
url: string,
...
}
// Modified version of react-router-dom ContextRouter type for example purposes
type ContextRouter = {|
history: string,
location: string,
match: Match,
|};
// Not working how I want it to
type Props = {|
...$Shape<ContextRouter>,
match: { params: { [key: string]: ?string, ... } }
|};
最终目标是让这些场景正常工作:
// I want this to be allowed (with history and location optional)
({
match: {params: {id: '7'}}
}:Props);
// I want this to throw an error on the 'unknown' property
({
match: {params: {id: '7'}},
unknown: true
}:Props);
我希望 ContextRouter
中的所有内容都是可选的。如果缺少 ContextRouter
的任何属性,我的示例目前会抛出错误。
22: ({ ^ Cannot cast object literal to `Props` because property `history` is missing in object literal [1] but exists in `Props` [2].
References:
22: ({ ^ [1]
24: }:Props);
^ [2]
我在这里做了一个起点:flow playground
我正在尝试将第三方库提供的对象定义作为我的确切对象类型的可选属性进行传播。
示例:
type Match = {
params: { [key: string]: ?string, ... },
isExact: boolean,
path: string,
url: string,
...
}
// Modified version of react-router-dom ContextRouter type for example purposes
type ContextRouter = {|
history: string,
location: string,
match: Match,
|};
// Not working how I want it to
type Props = {|
...$Shape<ContextRouter>,
match: { params: { [key: string]: ?string, ... } }
|};
最终目标是让这些场景正常工作:
// I want this to be allowed (with history and location optional)
({
match: {params: {id: '7'}}
}:Props);
// I want this to throw an error on the 'unknown' property
({
match: {params: {id: '7'}},
unknown: true
}:Props);
我希望 ContextRouter
中的所有内容都是可选的。如果缺少 ContextRouter
的任何属性,我的示例目前会抛出错误。
22: ({ ^ Cannot cast object literal to `Props` because property `history` is missing in object literal [1] but exists in `Props` [2].
References:
22: ({ ^ [1]
24: }:Props);
^ [2]
我在这里做了一个起点:flow playground