我使用的快递-在我的应用next.js,我通过一些配置给客户端从服务器通过res.locals,我的项目是打字稿,我使用“@类型/下一个”:“^ 8.0。 6”。问题:打字稿说 "Property 'locals' does not exist on type 'ServerResponse | Partial<ServerResponse>'."预期答案: - 一种将“NextContext.res”或“NextResponse”覆盖为等于Express.res而不是的方法http.ServerResponse - 一种将 NextResponse 覆盖为 add 的方法locals: any | {x:strong}。这是我们所拥有的以及如何重现的简化示例:import React from 'react';import Document, { Head, Main, NextScript } from 'next/document';type Props = { locals: {};}export default class MyDocument extends Document<Props> { render() { const { locals } = this.props; return ( <html> <Head> <ComponentUsingLocalsInfo locals={locals} /> </Head> <body> <Main /> <NextScript /> </body> </html> ) }}MyDocument.getInitialProps = async (ctx) => { const { locals } = ctx.res; const initialProps = await Document.getInitialProps(ctx); return { ...initialProps, locals, }}
2 回答
![?](http://img1.sycdn.imooc.com/533e51f30001edf702000200-100-100.jpg)
波斯汪
TA贡献1811条经验 获得超4个赞
对于快速解决方案,这对我有用:
import { Response } from 'express';
MyDocument.getInitialProps = async (ctx) => {
const { locals } = (ctx.res as Response);
}
![?](http://img1.sycdn.imooc.com/533e4ce900010ae802000200-100-100.jpg)
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
你总是可以像这样创建一个自定义界面:
export interface Custom_Initial_Props extends Omit<NextPageContext, 'res'> {
res: Express.Request;
}
添加回答
举报
0/150
提交
取消