将 i18next 实例传递给 I18nextProvider 时出现类型错误
Type error while passing i18next instance to I18nextProvider
错误:
Type 'Promise' is missing the following properties from type 'i18n': t, init, loadResources, use, and 32 more.ts(2740)
index.d.ts(344, 3): The expected type comes from property 'i18n' which is declared here on type 'IntrinsicAttributes & I18nextProviderProps & { children?: ReactNode; }'
i18n/intex.ts
import { NODE_ENV } from "@/config";
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
export default i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
providers/app.tsx
import { AuthProvider } from "@/lib/auth";
import { queryClient } from "@/lib/react-query";
import { ReactNode, Suspense } from "react";
import { QueryClientProvider } from "react-query";
import { BrowserRouter as Router } from "react-router-dom";
import { ReactQueryDevtools } from "react-query/devtools";
import { ConfigProvider } from "antd";
import en from "antd/lib/locale/en_US";
import { I18nextProvider } from "react-i18next";
import i18n from "@/i18n";
type AppProviderProps = {
children: ReactNode;
};
export const AppProvider = ({ children }: AppProviderProps) => {
return (
<Suspense fallback={<div>Loading...</div>}>
<ConfigProvider locale={en}>
<QueryClientProvider client={queryClient}>
{import.meta.env.DEV && <ReactQueryDevtools />}
<AuthProvider>
<I18nextProvider i18n={i18n}>
<Router>{children}</Router>
</I18nextProvider>
</AuthProvider>
</QueryClientProvider>
</ConfigProvider>
</Suspense>
);
};
// this returns a promise not an i18n instance
export default i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
这应该有效:
// don't care for the promise
i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
// just return the instance you have
export default i18n;
或等待承诺(这仅适用于顶级等待支持):
export default await i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
错误:
Type 'Promise' is missing the following properties from type 'i18n': t, init, loadResources, use, and 32 more.ts(2740) index.d.ts(344, 3): The expected type comes from property 'i18n' which is declared here on type 'IntrinsicAttributes & I18nextProviderProps & { children?: ReactNode; }'
i18n/intex.ts
import { NODE_ENV } from "@/config";
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
export default i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
providers/app.tsx
import { AuthProvider } from "@/lib/auth";
import { queryClient } from "@/lib/react-query";
import { ReactNode, Suspense } from "react";
import { QueryClientProvider } from "react-query";
import { BrowserRouter as Router } from "react-router-dom";
import { ReactQueryDevtools } from "react-query/devtools";
import { ConfigProvider } from "antd";
import en from "antd/lib/locale/en_US";
import { I18nextProvider } from "react-i18next";
import i18n from "@/i18n";
type AppProviderProps = {
children: ReactNode;
};
export const AppProvider = ({ children }: AppProviderProps) => {
return (
<Suspense fallback={<div>Loading...</div>}>
<ConfigProvider locale={en}>
<QueryClientProvider client={queryClient}>
{import.meta.env.DEV && <ReactQueryDevtools />}
<AuthProvider>
<I18nextProvider i18n={i18n}>
<Router>{children}</Router>
</I18nextProvider>
</AuthProvider>
</QueryClientProvider>
</ConfigProvider>
</Suspense>
);
};
// this returns a promise not an i18n instance
export default i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
这应该有效:
// don't care for the promise
i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});
// just return the instance you have
export default i18n;
或等待承诺(这仅适用于顶级等待支持):
export default await i18n.use(initReactI18next).init({
fallbackLng: "en",
debug: NODE_ENV === "development"
});