简体   繁体   English

NextJS _app.tsx 组件和 pageProps 应该是什么 TypeScript 类型?

[英]What TypeScript type should NextJS _app.tsx Component and pageProps be?

Here's the default _app.tsx from NextJS:这是 NextJS 的默认 _app.tsx:

function MyApp({ Component, pageProps }) {
  return (
    <Component {...pageProps} />
  )
}

The problem is, as soon as you switch to TypeScript, you get a warning under ES6Lint that these types are intrinsicly set to type 'any'.问题是,一旦您切换到 TypeScript,您就会在 ES6Lint 下收到警告,指出这些类型本质上设置为“任何”类型。 That being said, I can't figure out what type to set these two to that wont cause more errors later of mismatched types.话虽如此,我无法弄清楚将这两个设置为哪种类型,以后不会导致更多错误类型不匹配。 What TypeScript types should I cast these two as?我应该将这两个转换为什么 TypeScript 类型?

You could import the types from nextjs .您可以从nextjs导入类型。

import { AppProps } from 'next/app';

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

Improved AppProps to allow customisable pageProps改进AppProps以允许可自定义pageProps

NextJS provides an AppProps type out of the box that you can easily import and use in your _app component. NextJS 提供了一个开箱即用的AppProps类型,您可以轻松地在_app组件中导入和使用它。 This will provide the types you asked for.这将提供您要求的类型。 However, as mentioned in another comment, this type restricts pageProps to the any type by default, which may not be what you want.但是,正如另一条评论中提到的,这种类型默认将pageProps限制为any类型,这可能不是您想要的。

If you want more control over this, it turns out that AppProps does take a type argument, but only passes it down to Component , leaving pageProps as the any type no matter what.如果您想对此进行更多控制,事实证明AppProps确实接受了类型参数,但仅将其传递给Component ,无论如何都将pagePropsany类型。

We can fix this with a tweak to the AppProps type that actually passes it's type argument to pageProps as well.我们可以通过调整AppProps类型来解决这个问题,该类型实际上也将它的类型参数传递给pageProps See working example below.请参阅下面的工作示例。

_app.tsx

// importing the provided NextJS type
import type { AppProps as NextAppProps } from "next/app";

// modified version - allows for custom pageProps type, falling back to 'any'
type AppProps<P = any> = {
  pageProps: P;
} & Omit<NextAppProps<P>, "pageProps">;

// use the new type like so, replacing 'CustomPageProps' with whatever you want
export default function App({
  Component,
  pageProps,
}: AppProps<CustomPageProps>) {
  //...
}

If you still getting the warning of 'Missing return type on function', just add return type JSX.Element如果您仍然收到“缺少函数返回类型”的警告,只需添加返回类型 JSX.Element

import { AppProps } from 'next/app'

export default function MyApp({ Component, pageProps }: AppProps): JSX.Element {
  return <Component {...pageProps} />
}

In case you need the err object too:如果您也需要err对象:

export default function App({
  Component,
  pageProps,
  err,
}: AppProps & { err: any }) {
  // Workaround for https://github.com/vercel/next.js/issues/8592
  return <Component {...pageProps} err={err} />
}

I believe you need types that will be caught up by getInitialProps as well.我相信你需要的类型也会被getInitialProps赶上。 There they are:它们是:

type Props<P> = AppProps & P;

interface AppType<P> {
  (props: Props<P>): ReactElement;
  getInitialProps?: (context: AppContext) => Props<P> | Promise<Props<P>>;
}

Documentation: https://nextjs.org/docs/basic-features/typescript#custom-app文档: https://nextjs.org/docs/basic-features/typescript#custom-app

import type { AppProps } from 'next/app'

export default function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

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

相关问题 无法在 app.tsx 中导入 tsx 组件 - Unable to import tsx component in app.tsx 如何在 _app.tsx (NextJS) 中使用 Redux state - How to use Redux state in _app.tsx (NextJS) 如何使用 _app.tsx 配置 redux? - How to configure redux with _app.tsx? &#39;Router&#39; 不能用作 JSX 组件。 在我在 index.tsx 中添加组件 Msal 组件和在 app.tsx 中添加 PageLayout 之前,它工作正常 - 'Router' cannot be used as a JSX component. It was working fine until I added the component Msal componant in index.tsx and PageLayout in app.tsx 如何在 React 中使用 App.tsx 而不是 App.js? - How do I use App.tsx instead of App.js in React? 缺少道具验证 - _app.js 中的组件和 pageProps。 如何解决? - Missing props validation - Component and pageProps in _app.js. How to fix it? 为什么将 app.js 更改为 app.tsx 会破坏我的 Next.js 项目? - Why does changing app.js to app.tsx break my Next.js project? Typescript 中的参数类型应该是什么,它在功能组件 Typescript 中采用 Obj 数组 - What should be the type of parameter in Typescript which take Array of Obj in a functional component Typescript 在打字稿中应该定义什么类型的超时 - what type should timeout be defined at in typescript React children 应该设置为什么 TypeScript 类型? - What TypeScript type should React children be set to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM