如果reselect@4.1.5
您使用的是typescript@≥4.2
. 所以现在需要指定为:createSelector<Selectors extends SelectorArray, Result>
<Selectors extends SelectorArray, Result>
export type Selector<
// The state can be anything
State = any,
// The result will be inferred
Result = unknown,
// There are either 0 params, or N params
Params extends never | readonly any[] = any[]
// If there are 0 params, type the function as just State in, Result out.
// Otherwise, type it as State + Params in, Result out.
> = [Params] extends [never]
? (state: State) => Result
: (state: State, ...params: Params) => Result
export type SelectorArray = ReadonlyArray<Selector>
例子:
// get all posts
export const selectPosts = (state: TState): TPostsState => state.posts;
// get new posts
export const selectNewPosts = createSelector<
[Selector<TState, TPostsState>],
TPostData[]
>(
selectPosts,
(posts) => posts.filter(({ type }) => type === 'new'),
);
但一般来说,对于较新的 TS,您现在不应该手动指定类型,因为它们会被自动推断。
如果您得到TS4023: Exported variable 'X' has or is using the name '$CombinedState' from external module
,请参阅 Stackoverflow 答案https://stackoverflow.com/a/43901135/10963661或尝试"declaration": false
在文件中设置编译器选项tsconfig.json
。