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

标签 reactjs typescript react-intl react-localization

鉴于 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/

相关文章:

Typescript:异步 * 迭代器闭包未编译

typescript - React-intl,将 api 与 Typescript 一起使用

twitter-bootstrap - 将react-intl与react-bootstrap一起使用

reactjs - Redux:子操作后更新父组件数据

javascript - 向 firebase 用户添加新数据

javascript - 如何在 Global Jest 设置中查找别名?

react-intl - babel-plugin-react-intl : Extract strings to a single file

reactjs - Babel 7 升级 : @babel/helper-module-imports

javascript - ReactJS 导出到 Excel 工作表

javascript - 未捕获类型错误 : not a function for a private method in TypeScript