从module.exports = js文件创建Typescript定义文件

时间:2017-11-29 21:12:44

标签: typescript definitelytyped

我正在尝试为format-duration创建一个Typescript定义文件:

module.exports = (ms) => {
  let { days, hours, minutes, seconds } = parseMs(ms)
  seconds = addZero(seconds)
  if (days) return `${days}:${addZero(hours)}:${addZero(minutes)}:${seconds}`
  if (hours) return `${hours}:${addZero(minutes)}:${seconds}`
  return `${minutes}:${seconds}`
}

我尝试过几个不同的声明文件:

export default function (ms: number): string;

export default function formatDuration(ms: number): string;

export function formatDuration(ms: number): string;

虽然我在运行应用程序时从中获取了正确的类型信息,但我收到错误:

import formatDuration from "format-duration";

const formatted = formatDuration(123456);

Exception has occurred: TypeError TypeError: format_duration_1.default is not a function

如何正确定义此类javascript库的类型?

1 个答案:

答案 0 :(得分:1)

似乎正确的声明文件如下所示:

declare function formatDuration(ms: number): string;

export = formatDuration;

并按以下方式导入:

import formatDuration = require("format-duration");