从 module.exports = js 文件创建 Typescript 定义文件
Creating Typescript definition files from module.exports = js files
我正在尝试为 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 库正确定义类型?
看来正确的声明文件是这样的:
declare function formatDuration(ms: number): string;
export = formatDuration;
它是这样导入的:
import formatDuration = require("format-duration");
我正在尝试为 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 库正确定义类型?
看来正确的声明文件是这样的:
declare function formatDuration(ms: number): string;
export = formatDuration;
它是这样导入的:
import formatDuration = require("format-duration");