I am new to typescript and I really want my initialize on mount to be set to true, Does anyone know why it will only let me set it to false? Here is the error:
Type '{ children: Element; appId: string | undefined; serverUrl: string | undefined; initializeOnMount: true; }' is not assignable to type 'IntrinsicAttributes & MoralisProviderProps'.
Types of property 'appId' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2322)
The type signature of MoralisProvider is
const MoralisProvider: ({ children, appId: _appId, serverUrl: _serverUrl, jsKey, dangerouslyUseOfMasterKey, plugins, environment, getMoralis, options: { onAccountChanged },
initializeOnMount, }: MoralisProviderProps) => JSX.Element
The code for the component mounting
import MoralisProvider
import type { AppProps } from 'next/app';
import { MoralisProvider } from 'react-moralis';
function MyApp({ Component, pageProps }: AppProps) {
return (
<MoralisProvider
appId={process.env.NEXT_PUBLIC_MORALIS_APP_ID}
serverUrl={process.env.NEXT_PUBLIC_MORALIS_SERVER_ID}
initializeOnMount
>
<Component {...pageProps} />
</MoralisProvider>)
}
export default MyApp;
CodePudding user response:
The problem Typescript is highlighting is not with initializeOnMount
but with the appId
. Using process.env
returns an object with values that may or may not exist. You may want to extract the values before mounting and assert that they exist. You can use something simple such as
const getSetupEnv = (key: string): string => {
if (!process.env[key]) {
throw Error("handle me")
}
return process.env[key]
}
The tricky part is knowing what to do if it throws, but that depends on the rest of your project.
CodePudding user response:
all process.env values are string | undefined
rewrite using nullish coalescing to solve the problem
import MoralisProvider
import type { AppProps } from 'next/app';
import { MoralisProvider } from 'react-moralis';
function MyApp({ Component, pageProps }: AppProps) {
return (
<MoralisProvider
appId={process.env.NEXT_PUBLIC_MORALIS_APP_ID ?? "AppId Undefined Fallback"}
serverUrl={process.env.NEXT_PUBLIC_MORALIS_SERVER_ID ?? "ServerUrl Undefined Fallback"}
initializeOnMount
>
<Component {...pageProps} />
</MoralisProvider>)
}
export default MyApp;
tip
inject app with MoralisProviderProps. Make a types/next.d.ts
file then populate it as follows:
import type { NextComponentType, NextPageContext } from "next";
import type { AppInitialProps } from "next/app";
import type { Router } from "next/router";
import type { MoralisProviderProps } from 'react-moralis';
declare module "next/app" {
type AppProps<P = Record<string, unknown>> = {
Component: NextComponentType<NextPageContext, any, P>;
router: Router;
__N_SSG?: boolean | undefined;
__N_SSP?: boolean | undefined;
__N_RSC?: boolean | undefined;
pageProps: P & {
moralisProps: MoralisProviderProps;
};
};
}
if you only want the appId and serverUrl passed in as props, then make a custom type to pass in to _app
import type { NextComponentType, NextPageContext } from "next";
import type { AppInitialProps } from "next/app";
import type { Router } from "next/router";
import { MoralisProvider } from 'react-moralis';
export declare const customAppProps: {
appId: string | undefined;
serverUrl: string | undefined;
} = {
appId: process.env.NEXT_PUBLIC_MORALIS_APP_ID,
serverUrl: process.env.NEXT_PUBLIC_MORALIS_SERVER_ID
};
declare module "next/app" {
type AppProps<P = Record<string, unknown>> = {
Component: NextComponentType<NextPageContext, any, P>;
router: Router;
__N_SSG?: boolean | undefined;
__N_SSP?: boolean | undefined;
__N_RSC?: boolean | undefined;
pageProps: P & {
customProps: typeof customAppProps;
};
};
}
Then populate _app as follows
function MyApp({ Component, pageProps }: AppProps) {
return (
<MoralisProvider
appId={pageProps.customProps.appId}
serverUrl={pageProps.customProps.serverUrl}
initializeOnMount
>
<Component {...pageProps} />
</MoralisProvider>)
}
export default MyApp;
Note -- this is especially useful for global context providers like apollo client's normalized cache object or the status of a session or jwt for authentication