Make moment's typings for parsing stricter and also allow them to accept union types.

These changes do two things.

1. Replace very lax typings like `any[]` with stricter, more-correct versions. In particular,
the ISO_8601 constant, while /technically/ a void function, is actually an opaque sentinel
that a consumer should not know anything about it. The function type was replaced with a
sentinel type using the principle of "brands" that can be seen in the Typescript compiler:
https://github.com/Microsoft/TypeScript/blob/413d9a639f933df7539070b236c1677de8302a93/src/compiler/types.ts#L9

2. Replace the many overloads of the parsing methods with a smaller representative set that
uses union types instead. Aside from succinctness, this allows callers to provide a union
type as the argument, as long as it matches, which was not possible before (Typescript does
not explode the union type to see if overloads cover all the possibilities).
This commit is contained in:
Sean Kelley
2016-03-29 15:03:48 -07:00
parent ee9c524295
commit 1ec036686e
2 changed files with 13 additions and 25 deletions
+4 -16
View File
@@ -35,22 +35,10 @@ interface MomentTimezone {
(date: number, timezone: string): moment.Moment;
(date: number[], timezone: string): moment.Moment;
(date: string, timezone: string): moment.Moment;
(date: string, format: string, timezone: string): moment.Moment;
(date: string, format: string, strict: boolean, timezone: string): moment.Moment;
(date: string, format: string, language: string, timezone: string): moment.Moment;
(date: string, format: string, language: string, strict: boolean, timezone: string): moment.Moment;
(date: string, formats: string[], timezone: string): moment.Moment;
(date: string, formats: string[], strict: boolean, timezone: string): moment.Moment;
(date: string, formats: string[], language: string, timezone: string): moment.Moment;
(date: string, formats: string[], language: string, strict: boolean, timezone: string): moment.Moment;
(date: string, specialFormat: () => void, timezone: string): moment.Moment;
(date: string, specialFormat: () => void, strict: boolean, timezone: string): moment.Moment;
(date: string, specialFormat: () => void, language: string, timezone: string): moment.Moment;
(date: string, specialFormat: () => void, language: string, strict: boolean, timezone: string): moment.Moment;
(date: string, formatsIncludingSpecial: any[], timezone: string): moment.Moment;
(date: string, formatsIncludingSpecial: any[], strict: boolean, timezone: string): moment.Moment;
(date: string, formatsIncludingSpecial: any[], language: string, timezone: string): moment.Moment;
(date: string, formatsIncludingSpecial: any[], language: string, strict: boolean, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, strict: boolean, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, language: string, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, language: string, strict: boolean, timezone: string): moment.Moment;
(date: Date, timezone: string): moment.Moment;
(date: moment.Moment, timezone: string): moment.Moment;
(date: Object, timezone: string): moment.Moment;
+9 -9
View File
@@ -571,6 +571,12 @@ declare namespace moment {
yy: any;
}
interface MomentBuiltinFormat {
__momentBuiltinFormatBrand: any;
}
type MomentFormatSpecification = string | MomentBuiltinFormat | (string | MomentBuiltinFormat)[];
interface MomentStatic {
version: string;
fn: Moment;
@@ -578,14 +584,8 @@ declare namespace moment {
(): Moment;
(date: number): Moment;
(date: number[]): Moment;
(date: string, format?: string, strict?: boolean): Moment;
(date: string, format?: string, language?: string, strict?: boolean): Moment;
(date: string, formats: string[], strict?: boolean): Moment;
(date: string, formats: string[], language?: string, strict?: boolean): Moment;
(date: string, specialFormat: () => void, strict?: boolean): Moment;
(date: string, specialFormat: () => void, language?: string, strict?: boolean): Moment;
(date: string, formatsIncludingSpecial: any[], strict?: boolean): Moment;
(date: string, formatsIncludingSpecial: any[], language?: string, strict?: boolean): Moment;
(date: string, format?: MomentFormatSpecification, strict?: boolean): Moment;
(date: string, format?: MomentFormatSpecification, language?: string, strict?: boolean): Moment;
(date: Date): Moment;
(date: Moment): Moment;
(date: Object): Moment;
@@ -675,7 +675,7 @@ declare namespace moment {
/**
* Constant used to enable explicit ISO_8601 format parsing.
*/
ISO_8601(): void;
ISO_8601: MomentBuiltinFormat;
defaultFormat: string;
}