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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With