编辑2021/04/23:我下面的答案也过时了
有关使用新类型和命名导出键入数据获取方法的当前建议,请参见the Next.js docs.
tl;博士:
import { GetStaticProps, GetStaticPaths, GetServerSideProps } from 'next'
export const getStaticProps: GetStaticProps = async (context) => {
// ...
}
export const getStaticPaths: GetStaticPaths = async () => {
// ...
}
export const getServerSideProps: GetServerSideProps = async (context) => {
// ...
}
过时(但功能性)答案:
以上答案从明年起就过时了.js现在正式支持TypeScript(公告here)
这个版本的一部分是更好的TypeScript类型,还有很多Next.js是用TypeScript本身编写的.这意味着@types/next
美元的一揽子计划将被弃用,取而代之的是下一个正式的计划.js打字.
相反,您应该导入NextPage
type并将其分配给您的组件.您也可以使用NextPageContext
类型键入getInitialProps
.
import { NextPage, NextPageContext } from 'next';
const MyComponent: NextPage<MyPropsInterface> = props => (
// ...
)
interface Context extends NextPageContext {
// any modifications to the default context, e.g. query types
}
MyComponent.getInitialProps = async (ctx: Context) => {
// ...
return props
}