.d.ts在TypeScript 2.6.1之后没有编译

时间:2017-11-03 11:01:46

标签: typescript

我在这个环境模块中使用TypeScript版本2.5用于suncalc包:

// Note: incomplete
// https://www.npmjs.com/package/suncalc

declare module "suncalc" {
  interface suncalcResult {
    solarNoon: Date;
    nadir: Date;
    sunrise: Date;
    sunset: Date;
    sunriseEnd: Date;
    sunsetStart: Date;
    dawn: Date;
    dusk: Date;
    nauticalDawn: Date;
    nauticalDusk: Date;
    nightEnd: Date;
    night: Date;
    goldenHourEnd: Date;
    goldenHour: Date;
  }

  function sunCalc(date: Date, latitude: number, longitude: number): suncalcResult;

  export = { // COMPLAINING HERE <--------------------------- line 24
    getTimes: sunCalc
  };
}

在TypeScript 2.5中,我得到了一个名为suncalc.d.ts的文件,编译时没有错误。当我升级到2.6时,它开始受到责备:

message: 'The expression of an export assignment must be an identifier or qualified name in an ambient context.'
at: '24,12'
source: 'ts'

但是,在changelog of TypeScript中,没有任何关于环境模块的更改。

为什么现在不编译?我该如何在TS2.6中编写导出?

1 个答案:

答案 0 :(得分:6)

breaking changes页面上提到了这一点。像所讨论的那样的导出分配是可执行代码,而2.6.1是more strict in enforcing general rule that executable code is not allowed in declaration files

重写声明的建议方法是声明一个具有显式类型的变量并导出变量:

const _exported: { getTimes: sunCalc };
export = _exported;