分析节点的返工类型定义

Rework type definitions for analytics-node

该库的类型定义公开了 class Analytics

所以,来源是下一个

class Analytics {}

module.exports = Analytics

类型定义很好,很快就可以了

declare namespace AnalyticsNode {
  export class Analytics {}
}

export = AnalyticsNode.Analytics

但是在这些声明之后,使用库的唯一方法是

import Analytics = require('analytics-node')

如何在本地覆盖类型定义以使其以 ES6 导入方式工作?

我尝试声明一个模块

declare module 'analytics-node' {
  // export default
  // export
  // export = 
}

但这行不通。 (import * as Analytics from 'analytics-node' 可以访问函数,但 new Analytics 不会在编译器中引发错误)

我尝试遵循有关 module class definitions 的指南,但没有成功。

看起来问题出在模块的 CommonJS(node) 和 ES 实现之间的差异。

TS esModuleInterop 中有一个标志可以消除两个系统之间的差异。使用该标志,可以使用 import module from 'module' 而不是 import * as module from 'module'

这是 github issue Andrew Fong 的笔记。

absent esModuleInterop, the only way to use that syntax is for analytics-node to move to a default ES module export. Here's the thing though -- unless Node itself forces a migration to ES modules, the CommonJS single export remains perfectly valid. There technically isn't anything to fix. esModuleInterop isn't a hacky workaround. It's actually how, AFAICT, the TS devs want us to type these scenarios.