关于未找到状态且不可分配给类型的 next-redux-wrapper 的 Typescript 警告
Posted
技术标签:
【中文标题】关于未找到状态且不可分配给类型的 next-redux-wrapper 的 Typescript 警告【英文标题】:Typescript Warning for next-redux-wrapper regarding state not found and not assignable to types 【发布时间】:2021-09-11 15:36:40 【问题描述】:我正在使用 next-redux-wrapper,但出现以下 2 种类型错误。有人遇到过这个问题,你是怎么解决的?除此之外,代码正在运行,我可以在我的 redux-devtools-extension 中看到我的状态。谢谢
import useSelector from "react-redux";
import Layout from "../components/Layout";
import getProjects from "../redux/actions/projectActions";
import wrapper from "../redux/store";
const Redux = () =>
// Warning 1: Property 'allProjects' does not exist on type 'DefaultRootState'.ts(2339)
const projects = useSelector((state) => state.allProjects);
console.log("res", projects);
return (
<Layout>
<h1>Redux</h1>
</Layout>
);
;
export const getServerSideProps =
// Warning 2: Type '( req : GetServerSidePropsContext<ParsedUrlQuery>) => Promise<void>' is not assignable to type 'GetServerSideProps<any, ParsedUrlQuery>'.
Type 'Promise<void>' is not assignable to type 'Promise<GetServerSidePropsResult<any>>'.
Type 'void' is not assignable to type 'GetServerSidePropsResult<any>'.ts(2322)
wrapper.getServerSideProps(
(store) =>
async ( req ) =>
(await store.dispatch(getProjects(req))) as any;
);
export default Redux;
【问题讨论】:
【参考方案1】:我在下面做了,ts 警告消失了
我不确定它是否正确,但到目前为止似乎还可以
import RootStateOrAny, useSelector from "react-redux";
import Layout from "../components/Layout";
import getProjects from "../redux/actions/projectActions";
import wrapper from "../redux/store";
const Redux = () =>
const projects = useSelector(
(state: RootStateOrAny) => state.allProjects
);
console.log("res", projects);
return (
<Layout>
<h1>Redux</h1>
</Layout>
);
;
export const getServerSideProps = wrapper.getServerSideProps(
(store) =>
async ( req ) =>
await store.dispatch(getProjects(req));
return
props: ,
;
);
export default Redux;
【讨论】:
以上是关于关于未找到状态且不可分配给类型的 next-redux-wrapper 的 Typescript 警告的主要内容,如果未能解决你的问题,请参考以下文章
使用反应成帧器类型'()=> void'的打字稿错误不可分配给类型'未定义'
与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数(() => 状态属性)'
Mongoose TypeScript ObjectId 转换 - 类型“字符串”不可分配给类型“条件<ObjectId |未定义>'
Mongoose TypeScript ObjectId 转换 - 类型“字符串”不可分配给类型“条件<ObjectId |未定义>'