gpt4 book ai didi

reactjs - 使来自 react-intl 的 FormattedMessage 中的 id 继承自自定义 TypeScript 接口(interface)以启用 VS IntelliSense 和类型检查

转载 作者:行者123 更新时间:2023-12-03 16:48:02 25 4
gpt4 key购买 nike

鉴于 react-localization没有日期和数字格式,严重依赖一位开发人员,我们决定改用 react-intl因为从长远来看,它似乎更安全。
https://github.com/stefalda/react-localization/graphs/contributors
我们之前的代码如下所示:
本地化服务.ts

import LocalizedStrings from 'react-localization';

import svSE from './languages/sv-SE';
import enUS from './languages/en-US';
import arSA from './languages/ar-SA';

export default new LocalizedStrings({
svSE,
enUS,
arSA
});
ILanguageStrings.ts
export interface ILanguageStrings {
appName: string
narration: string
language: string
}
en-US.ts
import { ILanguageStrings } from '../ILanguageStrings';

const language: ILanguageStrings = {
appName: "Our App",
narration: "Narration",
language: "Language"
}

export default language;
然后可以导入本地化和 ILanguageStrings通过 Visual Studio 中的 IntelliSense 可见并由 TypeScript 验证。
import localization from '../services/localizationService';
enter image description here
但是使用 FormattedMessage来自 react-intl idstring | number | undefined .我们仍然使用语言文件,所以我们如何确定 idILanguageStrings不破坏 react-intl 中的原始类型定义?
enter image description here
我尝试使用 TypeScript 声明合并和合并接口(interface),但我只能在那里添加新成员而不能更改 id 属性。 “有效”字符串也不被视为正确。
react 应用程序 env.d.ts:
import * as reactIntl from 'react-intl';

declare module 'react-intl' {
export interface MessageDescriptor {
id?: ILanguageStrings;
idTest: ILanguageStrings
}
}
enter image description here
https://github.com/microsoft/TypeScript/issues/10859
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-interfaces

最佳答案

我之前在使用 react-intl 时遇到了同样的问题与 typescript .我的解决方案是简单地创建一个为 id 提供适当类型的包装器组件。 . id类型应该是 keyof支持最多的语言配置对象。
假设文件内容./languages/en-US有这样的东西

{
"AUTH.GENERAL.FORGOT_BUTTON": "Forgot Password",
"AUTH.LOGIN.TITLE": "Login Account",
"AUTH.FORGOT.TITLE": "Forgotten Password?",
"AUTH.REGISTER.TITLE": "Sign Up",
"AUTH.VALIDATION.INVALID": "{name} is not valid",
"AUTH.VALIDATION.REQUIRED": "{name} is required",
"AUTH.VALIDATION.NOT_FOUND": "The requested {name} is not found",
"AUTH.VALIDATION.INVALID_LOGIN": "The login detail is incorrect",
"AUTH.VALIDATION.REQUIRED_FIELD": "Required field",
"AUTH.VALIDATION.INVALID_FIELD": "Field is not valid",
"MENU.DASHBOARD": "Dashboard",
"MENU.PRODUCT": "Product",
"TOPBAR.GREETING": "Hi,",
...
}
I18nProvider.tsx
import React from "react";
import { IntlProvider } from "react-intl";
import svSE from './languages/sv-SE';
import enUS from './languages/en-US';
import arSA from './languages/ar-SA';

// In this example, english has the most support, so it has all the keys
export type IntlMessageID = keyof typeof enUS;

export default function I18nProvider({ children }) {
return (
<IntlProvider locale="en" messages={enMessages}>
{children}
</IntlProvider>
);
}
FormattedMessage.tsx
import React from "react";
import { FormattedMessage as ReactFormattedMessage } from "react-intl";
import { IntlMessageID } from "./I18nProvider";

type FormattedMessageProps = {
id?: IntlMessageID;
defaultMessage?: string;
values?: Record<string, React.ReactNode>;
children?: () => React.ReactNode;
};

export default function FormattedMessage(props: FormattedMessageProps) {
return <ReactFormattedMessage {...props} />;
}
用法
import React from "react";
import I18nProvider from "./I18nProvider";
import FormattedMessage from "./FormattedMessage";

export default function App() {
return (
<I18nProvider>
<div className="App">
<FormattedMessage id="..." />
</div>
</I18nProvider>
);
}
这是结果
enter image description here
现场演示
在下面的演示中,您可以通过按 Ctrl 在编辑器中触发 IntelliSense + Space Edit React-Int - Message ID Type

关于reactjs - 使来自 react-intl 的 FormattedMessage 中的 id 继承自自定义 TypeScript 接口(interface)以启用 VS IntelliSense 和类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63883950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com