代码之家  ›  专栏  ›  技术社区  ›  lilezek

.d、 ts在TypeScript 2.6.1之后未编译

  •  4
  • lilezek  · 技术社区  · 7 年前

    我使用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 回复  |  直到 6 年前
        1
  •  7
  •   artem    7 年前

    这在 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;