属性 X 在类型 'GetServerSidePropsContext<ParsedUrlQuery, PreviewData>' 上不存在
Property X does not exist on type 'GetServerSidePropsContext<ParsedUrlQuery, PreviewData>'
我有以下内容:
type Session = {
bearer: string,
firstName: string,
lastName: string,
etc...
};
interface ServerContext extends GetServerSidePropsContext {
session: Session,
};
export type ServerProps<P extends { [key: string]: any } = { [key: string]: any }> = (
context: ServerContext
) => Promise<GetServerSidePropsResult<P>>;
export const requireAuth = (gssp: GetServerSideProps) => {
return async (ctx: GetServerSidePropsContext) => {
const { req } = ctx;
const session = req?.headers?.cookie ? await getSession(req?.headers?.cookie) : null;
if (!session) {
return {
redirect: { permanent: false, destination: '/login' }
};
};
const serverSession = { ...ctx, session };
return await gssp(serverSession);
};
};
然后在需要时,我按如下方式使用它:
export const getServerSideProps: ServerProps = requireAuth(async _ctx => {
const { session } = _ctx;
let account = session;
let stats = {};
try {
stats = fetchSomeData
} catch (error) {
console.log('Pages/Index Fetching Error: ', error);
}
return {
props: {
account: account,
stats: stats,
navbar: true,
footer: true,
},
};
});
问题:
const { session } = _ctx 输出以下错误:“属性 session does not exist on type 'GetServerSidePropsContext<ParsedUrlQuery, PreviewData>'”
解决方法:
如果我转到 node_modules/next/types/index.d.tsx 并且我只是将 session: Session 添加到 GetServerSidePropsContext,它工作得很好,但是如果我尝试像上面那样扩展接口,它似乎不起作用。
export const requireAuth = (gssp: ServerProps) => {
return async (ctx: GetServerSidePropsContext) => {
const { req } = ctx;
const session = req?.headers?.cookie ? await getSession(req?.headers?.cookie) : null;
if (!session) {
return {
redirect: { permanent: false, destination: '/login' }
};
};
const serverSession = { ...ctx, session };
return await gssp(serverSession);
};
};
应该使用 ServerProps 而不是 GetServerSideProps
我有以下内容:
type Session = {
bearer: string,
firstName: string,
lastName: string,
etc...
};
interface ServerContext extends GetServerSidePropsContext {
session: Session,
};
export type ServerProps<P extends { [key: string]: any } = { [key: string]: any }> = (
context: ServerContext
) => Promise<GetServerSidePropsResult<P>>;
export const requireAuth = (gssp: GetServerSideProps) => {
return async (ctx: GetServerSidePropsContext) => {
const { req } = ctx;
const session = req?.headers?.cookie ? await getSession(req?.headers?.cookie) : null;
if (!session) {
return {
redirect: { permanent: false, destination: '/login' }
};
};
const serverSession = { ...ctx, session };
return await gssp(serverSession);
};
};
然后在需要时,我按如下方式使用它:
export const getServerSideProps: ServerProps = requireAuth(async _ctx => {
const { session } = _ctx;
let account = session;
let stats = {};
try {
stats = fetchSomeData
} catch (error) {
console.log('Pages/Index Fetching Error: ', error);
}
return {
props: {
account: account,
stats: stats,
navbar: true,
footer: true,
},
};
});
问题: const { session } = _ctx 输出以下错误:“属性 session does not exist on type 'GetServerSidePropsContext<ParsedUrlQuery, PreviewData>'”
解决方法: 如果我转到 node_modules/next/types/index.d.tsx 并且我只是将 session: Session 添加到 GetServerSidePropsContext,它工作得很好,但是如果我尝试像上面那样扩展接口,它似乎不起作用。
export const requireAuth = (gssp: ServerProps) => {
return async (ctx: GetServerSidePropsContext) => {
const { req } = ctx;
const session = req?.headers?.cookie ? await getSession(req?.headers?.cookie) : null;
if (!session) {
return {
redirect: { permanent: false, destination: '/login' }
};
};
const serverSession = { ...ctx, session };
return await gssp(serverSession);
};
};
应该使用 ServerProps 而不是 GetServerSideProps