gpt4 book ai didi

reactjs - 为 Reselect createSelector 函数分配正确的类型

转载 作者:行者123 更新时间:2023-12-03 17:37:47 24 4
gpt4 key购买 nike

编辑:添加了 package.json 摘录

我正在尝试在现有的 React 项目中实现 typescript ,但我遇到了 Reselect 的困难图书馆。
Typescript 编译器坚持导入 createSelector 函数的第一个定义并且无法识别我的函数的签名对应于另一个定义。

它导入的定义:

export function createSelector<S, R1, T>(
selector: Selector<S, R1>,
combiner: (res: R1) => T,
): OutputSelector<S, T, (res: R1) => T>;

我想使用的一个:
export function createSelector<S, R1, R2, T>(
selectors: [Selector<S, R1>,
Selector<S, R2>],
combiner: (res1: R1, res2: R2) => T,
): OutputSelector<S, T, (res1: R1, res2: R2) => T>;

这是我的实际代码:
// groups.selectors.tsx

import { Selector, createSelector } from 'Reselect';
import { IGroup, IQuestionnaire, IGroupReselect, IState } from '../interfaces';

const getGroups:Selector<IState, IGroup[]> = state => state.groups;
const getQuestionnaires:Selector<IState, IQuestionnaire[]> = state => state.questionnaires;

export const groups = createSelector<IState, IGroup[], IQuestionnaire[], IGroupReselect>(
[getGroups, getQuestionnaires],
(g, q) => {

return g.map(group => Object.assign(
{},
group,
{questionnaires: group.questionnairesIds.map(id => q.find(q => q.id === id))}
));
}
);

如果我有任何帮助,这是我的 ts.config:
{
"compilerOptions": {
"module": "es6",
"target": "es6",
"outDir": ".temp",
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"noImplicitAny": false,
"sourceMap": false,
"jsx": "preserve",
"strict": true,
"moduleResolution": "node"
},
"exclude": [
"node_modules"
],
"files": [
"typings.d.ts"
]
}

我对 TypeScript 并不完全满意,所以我的实现肯定有问题。让我感到困扰的是,如果我编写最简单的重新选择器,即只有 1 个选择器和一个 arity 1 的组合器,它会通过类型检查,这给我的印象是编译器没有正确选择重载函数中的正确定义重新选择的 index.d.ts

这是我的 package.json 的相关部分:
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-redux": "^5.0.5",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"redux": "^3.6.0",
"redux-devtools-extension": "^2.13.2",
"redux-thunk": "^2.2.0",
"reselect": "^3.0.1"
},
"devDependencies": {
"@types/react": "^15.0.25",
"@types/react-router-dom": "^4.0.4",
"@types/redux-thunk": "^2.1.0",
"typescript": "^2.3.3"
},

最佳答案

我可能错了,但从这里开始
this issue ,这似乎是由语言如何推断您的异构数组引起的[getGroups, getQuestionnaires]作为数组而不是元组。

强制元组类型的第一个参数将使其工作但会导致不必要的样板

const getGroups:Selector<IState, IGroup[]> = state => state.groups;
const getQuestionnaires:Selector<IState, IQuestionnaire[]> = state =>
state.questionnaires;

const selectors: [Selector<IState, IGroup[]>, Selector<IState, IQuestionnaire[]>] = [getGroups, getQuestionnaires]

export const groups = createSelector<IState, IGroup[], IQuestionnaire[], IGroupReselect>(
selectors,
(g, q) => {

return g.map(group => Object.assign(
{},
group,
{questionnaires: group.questionnairesIds.map(id => q.find(q => q.id === id))}
));
}
);

关于reactjs - 为 Reselect createSelector 函数分配正确的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44289468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com