Skip to content

Commit ed529ea

Browse files
cspotcodeellenaua
andauthored
Yet another PR adding TypeScript declarations (#530)
* Adds TypeScript declarations for moment-timezone * Remove unnecessary `moment.` qualification in typings * Move exported types to top-level so that they can all be imported as named imports - Zone class must exist at `tz.Zone` because that's where it's exposed at runtime, but the `Zone` interface is still re-exported as a named export * tsc invocation always uses locally installed tsc Co-authored-by: Elena Sharovar <[email protected]>
1 parent d50bf27 commit ed529ea

7 files changed

+461
-5
lines changed

Gruntfile.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ module.exports = function(grunt) {
4545

4646
clean: {
4747
data: ['temp']
48+
},
49+
50+
exec: {
51+
'typing-tests': './node_modules/.bin/tsc --project ./typing-tests'
4852
}
4953
});
5054

@@ -54,11 +58,13 @@ module.exports = function(grunt) {
5458
grunt.loadNpmTasks('grunt-contrib-jshint');
5559
grunt.loadNpmTasks('grunt-contrib-uglify');
5660
grunt.loadNpmTasks('grunt-contrib-clean');
61+
grunt.loadNpmTasks('grunt-exec');
62+
63+
grunt.registerTask('release', ['jshint', 'data', 'nodeunit', 'typing-tests', 'build', 'uglify']);
5764

58-
/** creates a release */
59-
grunt.registerTask('release', ['jshint', 'data', 'nodeunit', 'build', 'uglify']);
65+
grunt.registerTask('releaseNoData', ['jshint', 'nodeunit', 'typing-tests', 'build', 'uglify']);
6066

61-
grunt.registerTask('releaseNoData', ['jshint', 'nodeunit', 'build', 'uglify']);
67+
grunt.registerTask('typing-tests', ['exec:typing-tests']);
6268

63-
grunt.registerTask('default', ['jshint', 'nodeunit']);
69+
grunt.registerTask('default', ['jshint', 'nodeunit', 'typing-tests']);
6470
};

index.d.ts

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import * as moment from 'moment';
2+
3+
// require('moment-timezone') === require('moment')
4+
export = moment;
5+
6+
declare module 'moment' {
7+
interface Moment {
8+
/** Set the timezone and update the offset */
9+
tz(zone: string): Moment;
10+
/** Return the timezone name or undefined if a zone has not yet been set */
11+
tz(): string | undefined;
12+
13+
/** Get the zone abbreviation. This is what moment.js uses when formatting the z token. */
14+
zoneAbbr(): string;
15+
16+
/** Get the zone long form name. This is what moment.js uses when formatting the zz token. */
17+
zoneName(): string;
18+
}
19+
20+
// Match normal moment constructor but with an extra timezone argument
21+
// Here's a copy-paste of the normal moment constructor's signature, from https://github.com/moment/moment/blob/develop/moment.d.ts#L1-L2
22+
// declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, strict?: boolean): moment.Moment;
23+
// declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, language?: string, strict?: boolean): moment.Moment;
24+
25+
// Should be sorted from tightest to loosest. TypeScript picks the first signature that matches, going top to bottom.
26+
27+
/** create a moment with a time zone */
28+
function tz(inp: MomentInput, format: MomentFormatSpecification, language: string, strict: boolean, zone: string): Moment;
29+
/** create a moment with a time zone */
30+
function tz(inp: MomentInput, format: MomentFormatSpecification, language: string, zone: string): Moment;
31+
/** create a moment with a time zone */
32+
function tz(inp: MomentInput, format: MomentFormatSpecification, strict: boolean, zone: string): Moment;
33+
/** create a moment with a time zone */
34+
function tz(inp: MomentInput, format: MomentFormatSpecification, zone: string): Moment;
35+
/** create a moment with a time zone */
36+
function tz(inp: MomentInput, zone: string): Moment;
37+
/** create a moment with a time zone */
38+
function tz(zone?: string): Moment;
39+
40+
namespace tz {
41+
/** Version of moment-timezone */
42+
const version: string;
43+
44+
/**
45+
* Change the default timezone of newly created Moment instances.
46+
* By default new instances are created in the local timezone.
47+
*/
48+
function setDefault(zone: string): typeof moment;
49+
50+
/** Reset the default timezone to local. */
51+
function setDefault(): typeof moment;
52+
53+
/**
54+
* Retrieve or guess the user's timezone. Uses the browser's Internationalization API if available.
55+
* Otherwise, guesses by sampling offsets from different points in time and comparing them to available zone data.
56+
*/
57+
function guess(): string;
58+
59+
interface Zone extends UnpackedZone {}
60+
class Zone {
61+
/** Get the abbreviation for a given timestamp from a Zone. */
62+
abbr(timestamp: number): string;
63+
64+
/** Get the offset for a given timestamp from a Zone. */
65+
offset(timestamp: number): number;
66+
67+
/** Parse an offset for a timestamp constructed from Date.UTC in that zone. */
68+
parse(timestamp: number): number;
69+
}
70+
71+
/** Return a timezone by name or null if timezone by that name is not loaded. */
72+
function zone(name: string): Zone | null;
73+
74+
/** Add zone data for a timezone. */
75+
function add(packedZone: string): void;
76+
/** Add zone data for multiple timezones. */
77+
function add(packedZones: Array<string>): void;
78+
79+
/** Link two zone names to the same data */
80+
function link(packedLink: string): void;
81+
/** Add multiple links at once */
82+
function link(packedLinks: Array<string>): void;
83+
84+
/** load a bundle of zone data and links */
85+
function load(bundle: PackedZoneBundle): void;
86+
87+
/** get a list of all available time zone names */
88+
function names(): Array<string>;
89+
90+
/** Convert a packed string to an unpacked zone data object */
91+
function unpack(packedZone: string): UnpackedZone;
92+
/** Convert a base 60 string to a base 10 number. */
93+
function unpackBase60(base60String: string): number;
94+
}
95+
96+
type Zone = tz.Zone;
97+
98+
/** Parsed / unpacked zone data. */
99+
interface UnpackedZone {
100+
/** The uniquely identifying name of the time zone. */
101+
name: string;
102+
103+
/** zone abbreviations */
104+
abbrs: Array<string>;
105+
106+
/** (measured in milliseconds) */
107+
untils: Array<number | null>;
108+
109+
/** (measured in minutes) */
110+
offsets: Array<number>;
111+
}
112+
113+
/** Bundle of zone data and links for multiple timezones */
114+
interface PackedZoneBundle {
115+
version: string;
116+
zones: Array<string>;
117+
links: Array<string>;
118+
}
119+
120+
/** Bundle of zone data and links for multiple timezones */
121+
interface UnpackedZoneBundle {
122+
version: string;
123+
zones: Array<UnpackedZone>;
124+
links: Array<string>;
125+
}
126+
127+
}

moment-timezone-utils.d.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as moment from 'moment';
2+
3+
// require('moment-timezone') === require('moment')
4+
export = moment;
5+
6+
declare module 'moment' {
7+
namespace tz {
8+
/** Converts zone data in the unpacked format to the packed format. */
9+
function pack(unpackedObject: UnpackedZone): string;
10+
11+
/** Convert a base 10 number to a base 60 string. */
12+
function packBase60(input: number, precision?: number): string;
13+
14+
/** Create links out of two zones that share data.
15+
* @returns A new ZoneBundle with duplicate zone data replaced by links
16+
*/
17+
function createLinks(unlinked: UnpackedZoneBundle): PackedZoneBundle;
18+
19+
/**
20+
* Filter out data for years outside a certain range.
21+
* @return a new, filtered UnPackedZone object
22+
*/
23+
function filterYears(unpackedZone: UnpackedZone, startYear: number, endYear: number): UnpackedZone;
24+
/**
25+
* Filter out data for years outside a certain range.
26+
* @return a new, filtered UnPackedZone object
27+
*/
28+
function filterYears(unpackedZone: UnpackedZone, startAndEndYear: number): UnpackedZone;
29+
30+
/**
31+
* Combines packing, link creation, and subsetting of years into one simple interface.
32+
* Pass in an unpacked bundle, start year, and end year and get a filtered, linked, packed bundle back.
33+
*/
34+
function filterLinkPack(unpackedBundle: UnpackedZoneBundle, startYear: number, endYear: number): PackedZoneBundle;
35+
/**
36+
* Combines packing, link creation, and subsetting of years into one simple interface.
37+
* Pass in an unpacked bundle, start year, and end year and get a filtered, linked, packed bundle back.
38+
*/
39+
function filterLinkPack(unpackedBundle: UnpackedZoneBundle, startAndEndYear: number): PackedZoneBundle;
40+
}
41+
}

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"tz"
1616
],
1717
"main": "./index.js",
18+
"typings": "./index.d.ts",
1819
"engines": {
1920
"node": "*"
2021
},
@@ -34,7 +35,9 @@
3435
"grunt-contrib-clean": "^2.0.0",
3536
"grunt-contrib-jshint": "^2.1.0",
3637
"grunt-contrib-nodeunit": "^2.0.0",
37-
"grunt-contrib-uglify": "^4.0.1"
38+
"grunt-contrib-uglify": "^4.0.1",
39+
"grunt-exec": "^3.0.0",
40+
"typescript": "^3.5.1"
3841
},
3942
"jspm": {
4043
"main": "builds/moment-timezone-with-data",

0 commit comments

Comments
 (0)