类型不可分配给类型 'IntrinsicAttributes & MoralisProviderProps'
我是打字稿新手,我真的希望将安装时的初始化设置为 true,有谁知道为什么它只让我将其设置为 false?这是错误:
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)
MoralisProvider 的类型签名是
const MoralisProvider: ({ children, appId: _appId, serverUrl: _serverUrl, jsKey, dangerouslyUseOfMasterKey, plugins, environment, getMoralis, options: { onAccountChanged },
initializeOnMount, }: MoralisProviderProps) => JSX.Element
组件安装的代码
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以它只是期望一个字符串,因此使用模板文字解决了打字稿警告:
So it was just expecting a string and therefore using template literals resolved the typescript warning:
Typescript 突出显示的问题不是与
initializeOnMount
相关,而是与appId
相关。使用 process.env 返回一个对象,其值可能存在也可能不存在。您可能希望在安装之前提取这些值并断言它们存在。您可以使用简单的东西,例如棘手的部分是知道如果抛出异常该怎么办,但这取决于项目的其余部分。
The problem Typescript is highlighting is not with
initializeOnMount
but with theappId
. Usingprocess.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 asThe tricky part is knowing what to do if it throws, but that depends on the rest of your project.
所有 process.env 值都是
string | undefined
使用nullish合并重写来解决
提示
使用MoralisProviderProps注入应用程序的问题。创建一个
types/next.d.ts
文件,然后按如下方式填充它:如果您只想将 appId 和 serverUrl 作为 props 传入,则创建一个自定义类型以传递给 _app
然后将 _app 填充为注意
——这对于全局上下文提供者特别有用,例如 apollo 客户端的规范化缓存对象或用于身份验证的会话或 jwt 的状态
all process.env values are
string | undefined
rewrite using nullish coalescing to solve the problem
tip
inject app with MoralisProviderProps. Make a
types/next.d.ts
file then populate it as follows:if you only want the appId and serverUrl passed in as props, then make a custom type to pass in to _app
Then populate _app as follows
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