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

Ogg*_*las 5 typescript reactjs 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
});
Run Code Online (Sandbox Code Playgroud)

ILanguageStrings.ts

export interface ILanguageStrings {
    appName: string
    narration: string
    language: string
}
Run Code Online (Sandbox Code Playgroud)

en-US.ts

import { ILanguageStrings } from '../ILanguageStrings';

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

export default language;
Run Code Online (Sandbox Code Playgroud)

然后可以导入本地化并ILanguageStrings通过 Visual Studio 中的 IntelliSense 可见并通过 TypeScript 进行验证。

import localization from '../services/localizationService';
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但是使用FormattedMessagefromreact-intl id要么是string | number | undefined. 我们仍然使用语言文件,所以我们如何确保idILanguageStrings不破坏原始类型定义的情况下react-intl

在此处输入图片说明

我尝试使用 TypeScript 声明合并和合并接口,但我只能在那里添加新成员而不能更改 id 属性。“有效”字符串也被视为不正确。

react-app-env.d.ts:

import * as reactIntl from 'react-intl';

declare module 'react-intl' {
    export interface MessageDescriptor {
        id?: ILanguageStrings;
        idTest: ILanguageStrings 
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

https://github.com/microsoft/TypeScript/issues/10859

https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-interfaces

Nea*_*arl 3

react-intl我之前使用with时也遇到过同样的问题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,",
  ...
}
Run Code Online (Sandbox Code Playgroud)

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>
  );
}
Run Code Online (Sandbox Code Playgroud)

格式化消息.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} />;
}
Run Code Online (Sandbox Code Playgroud)

用法

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>
  );
}
Run Code Online (Sandbox Code Playgroud)

这是结果

在此输入图像描述

现场演示

在下面的演示中,您可以通过按Ctrl+在编辑器中触发IntelliSenseSpace

编辑 React-Int - 消息 ID 类型