From 1ec036686e70ad0d837ed2443684d54bca13af01 Mon Sep 17 00:00:00 2001 From: Sean Kelley Date: Tue, 29 Mar 2016 15:03:48 -0700 Subject: [PATCH] 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). --- moment-timezone/moment-timezone.d.ts | 20 ++++---------------- moment/moment-node.d.ts | 18 +++++++++--------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/moment-timezone/moment-timezone.d.ts b/moment-timezone/moment-timezone.d.ts index 85d5b4707..c1d7e2d23 100644 --- a/moment-timezone/moment-timezone.d.ts +++ b/moment-timezone/moment-timezone.d.ts @@ -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; diff --git a/moment/moment-node.d.ts b/moment/moment-node.d.ts index 13959bbf8..5199a64a2 100644 --- a/moment/moment-node.d.ts +++ b/moment/moment-node.d.ts @@ -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; }