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

如何为module.exports导出的函数及相关接口编写对应的.d.ts类型定义文件

Correct Type Definitions for Your CommonJS-Exported Function Module

Let's fix your .d.ts file so it properly matches your CommonJS export pattern (module.exports = function foo()) while supporting both clean ES module imports and exporting your required types. Here's the right approach:

Working Type Definition

declare module 'mymodule' {
  // Export your custom types first
  export type Language = 'it' | 'es' | 'de';
  export type Platform = 'linux' | 'winzoz';

  // Declare the function signature
  function foo(language: Language, platform: Platform, milliseconds?: number): void;

  // Support both CommonJS require() and ES module import (with esModuleInterop)
  export default foo;
  export = foo;
}

Why This Works

Let's break down why this solves your issues:

  • export = foo directly maps to your CommonJS module.exports = foo pattern, so const foo = require('mymodule') works without any extra config.
  • export default foo adds support for ES module-style import foo from 'mymodule' when you enable esModuleInterop in your tsconfig.json (more on that below).
  • Your Language and Platform types are exported at the module level, so you can import them separately just like any other TypeScript type.

Import Examples

CommonJS (no config needed)

const foo = require('mymodule');
foo('it', 'linux', 1000);

// Importing types (TypeScript only)
const foo = require('mymodule');
import type { Language, Platform } from 'mymodule';
const lang: Language = 'es';

ES Module (with esModuleInterop enabled)

import foo from 'mymodule';
import type { Language, Platform } from 'mymodule';

const lang: Language = 'de';
foo(lang, 'winzoz');

Fixing Your Previous Attempts

Let's go over why your earlier tries didn't work:

  1. First attempt (export function foo):
    This declares a module that exports a named foo function, but your actual JS module exports the function as the entire module itself. So you'd have to import it as import { foo } from 'mymodule', which doesn't match your CommonJS export.

  2. Second attempt (export default foo):
    Without esModuleInterop, TypeScript treats ES module import statements strictly and expects your CommonJS module to have a default property (which it doesn't—your module is the function itself).

  3. Third attempt (export = foo):
    This works for CommonJS, but requires the clunky import foo = require('mymodule') syntax for ES modules. Adding export default foo alongside it fixes this, and your types still remain exportable.

To make ES module imports work smoothly with CommonJS modules, add these to your tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}
  • esModuleInterop tells TypeScript to handle CommonJS modules as if they have a default export when using ES import.
  • allowSyntheticDefaultImports lets you write import foo from 'mymodule' without TypeScript throwing an error about missing a default export.

内容的提问来源于stack exchange,提问作者EuberDeveloper

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 08:17:30