简体   繁体   English

如何正确键入 Next.js getServerSideProps 作为 Function 声明

[英]How to Correctly Type Next.js getServerSideProps as a Function Declaration

I'm working with typing getServerSideProps ( guide docs , API reference docs ) but all of the documentation that I can find relates to declaring it as a function expression.我正在使用getServerSideProps指南文档API 参考文档),但我能找到的所有文档都与将其声明为 function 表达式有关。 I'm looking to code it as a function declaration but can't find any documentation on it and am having trouble getting it to work.我希望将其编码为 function 声明,但找不到任何关于它的文档并且无法使其正常工作。

As a function expression (this works):作为 function 表达式(有效):

import { GetServerSideProps } from 'next';

export type Data = {
  example: string,
};

export const getServerSideProps: GetServerSideProps<data: Data> = async (context) => {
    const data: Data = { example: '' };
    ...
    return {
        props: data
    };
}

This is where I'm at with it as a function declaration:这就是我将其作为 function 声明的地方:

import { GetServerSideProps, GetServerSidePropsContext } from 'next';

export type Data = {
    example: string,
};

export async function getServerSideProps(context: GetServerSidePropsContext): Promise<GetServerSideProps<data: Data>> {
    const data: Data = { example: '' };
    ...
    return {
        props: data
    };
}

TypeScript is happy with this as far as the declaration itself goes, but I get an error on the component declaration TS2339: Property 'example' does not exist on type 'never'.就声明本身而言,TypeScript 对此感到满意,但我在组件声明TS2339: Property 'example' does not exist on type 'never'.

export default function ExampleComponent(data: InferGetServerSidePropsType<typeof getServerSideProps>) {
    return (
        <>
            <p>{data.example}</p>
        </>
    );
}

What am I missing?我错过了什么? How can I properly use a function declaration for getServerSideProps and get the typing right?如何正确使用 function 声明getServerSideProps并正确输入?

Thanks.谢谢。

This works but see my other answer which makes better use of the types provided by Next.js.这行得通,但请参阅我的其他答案,它更好地利用了 Next.js 提供的类型。

Looks like I found an answer to this myself but if this can be done better then I'd appreciate hearing about it.看起来我自己找到了答案,但如果可以做得更好,那么我会很高兴听到它。

import { GetServerSidePropsContext } from 'next';

export async function getServerSideProps(context: GetServerSidePropsContext): Promise<{props: Data}> {
    const data: Data = { example: '' };
    ...
    return {
        props: data
    }
}

Ok pretty sure I found the correct answer to this now, having discovered the GetServerSidePropsResult type from next .好的,很确定我现在找到了正确的答案,从next发现了GetServerSidePropsResult类型。

import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';

export async function getServerSideProps(context: GetServerSidePropsContext): Promise<GetServerSidePropsResult<Data>> {
    const data: Data = { example: '' };
    ...
    return {
        props: data
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确键入 Next.js getStaticPaths 作为 Function 声明 - How to Correctly Type Next.js getStaticPaths as a Function Declaration getServerSideProps 上的 Supbase 策略 - Next.js - Supabase policies on getServerSideProps - Next.js Next.js GetServerSideProps 自定义包装器 TypeScript - Next.js GetServerSideProps custom wrapper TypeScript Next.js getServerSideProps 始终未定义 - Next.js getServerSideProps is always undefined 对于将 getServerSideProps 类型传递给 Next.Js 页面道具的最佳实践,哪个更好 - Which is better for best practice about passing getServerSideProps type to Next.Js page props 如何解决 Next.js 中 getServerSideProps 的“序列化”错误 - How can I resolve the error of 'serializing' from getServerSideProps in Next.js Next.js:getServerSideProps 无法使用 Next.js 和 Next-redux-wrapper 以及 TypeScript - Next.js: getServerSideProps not working using Next.js with Next-redux-wrapper with TypeScript Next.JS typescript 通过 getServerSideProps 将 props 返回给外部组件 - Next.JS typescript pass getServerSideProps returned props to external component 如何覆盖 Next.js `*.svg` 模块声明? - How to override Next.js `*.svg` module declaration? Vercel 在构建 Next.js 应用程序时找不到模块或类型声明 - Vercel cannot find module or type declaration when building Next.js app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM