You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建类型文件以将messageformat模块导出为类

Is Your messageformat Type Declaration Correct?

First off, you’re on the right track by creating a custom type declaration for the module—great work! Let’s go through a few adjustments to make it fully accurate and compatible with how the module behaves.

Key Issues in Your Current Declaration

  1. Module Export Alignment: The messageformat module exports the MessageFormat class directly as the module’s main export (via CommonJS module.exports = MessageFormat), but your current declaration uses a named export. This might cause TypeScript to misinterpret how you import the class with require().
  2. compile Method Return Type: The compile method doesn’t return a string directly—it returns a function that accepts a data object and returns the formatted string. Your current type declares it as returning string, which is incorrect.

Corrected Type Declaration

Here’s the adjusted version that fixes both issues, using TypeScript’s export = syntax to match CommonJS-style exports:

declare module "messageformat" {
  class MessageFormat {
    constructor(locale: string | string[]); // Support for single/multiple locales
    compile(messageString: string): (data?: Record<string, unknown>) => string;
  }

  export = MessageFormat;
}

Breakdown of Changes:

  • export = MessageFormat: This tells TypeScript that the entire module exports the MessageFormat class directly, which perfectly matches your const MessageFormat = require('messageformat'); import pattern.
  • Updated Constructor: The actual MessageFormat constructor accepts either a single locale string or an array of locales, so we expanded the type to string | string[] for better accuracy.
  • Fixed compile Return Type: Now it correctly declares that compile returns a function that takes an optional data object (of type Record<string, unknown>) and returns the formatted string. This aligns with real usage like:
    const fn = mf.compile('Hello {name}!');
    const text = fn({ name: 'Alice' }); // Returns "Hello Alice!"
    

Alternative: ES Module-Style Default Export

If you prefer using ES module syntax (import MessageFormat from 'messageformat'), you can write the declaration like this instead:

declare module "messageformat" {
  class MessageFormat {
    constructor(locale: string | string[]);
    compile(messageString: string): (data?: Record<string, unknown>) => string;
  }

  export default MessageFormat;
}

This works seamlessly with both ES imports and CommonJS require—TypeScript will automatically map require('messageformat') to the default export in this case.

Verifying the Declaration

To test it out:

  1. Save the declaration as messageformat.d.ts in your project (either in a @types folder or directly alongside your source code).
  2. Ensure your tsconfig.json includes the directory containing this file in include or typeRoots.
  3. Write your code as before—TypeScript should now correctly infer types for MessageFormat, its constructor, and the compile method.

内容的提问来源于stack exchange,提问作者Ramon de Klein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:54:32