Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.d.ts not compiling after TypeScript 2.6.1

Tags:

typescript

I was using TypeScript version 2.5 with this ambient module for suncalc package:

// 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
  };
}

In TypeScript 2.5, I got this file called suncalc.d.ts and compiled with no error. When I upgraded to 2.6, it started to blame:

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

However, in the changelog of TypeScript there is nothing about changes in ambient modules.

Why is not compiling now? How should I write the export in TS2.6?

like image 206
lilezek Avatar asked Mar 08 '23 16:03

lilezek


1 Answers

This is mentioned on the breaking changes page. Export assignments like the one in question are executable code, and 2.6.1 was made more strict in enforcing general rule that executable code is not allowed in declaration files.

The suggested way to rewrite the declaration is to declare a variable with explicit type and export the variable:

const _exported: { getTimes: sunCalc };
export = _exported;
like image 186
artem Avatar answered Mar 20 '23 15:03

artem