Skip to content

Commit cc12ab3

Browse files
committed
Add types to all files and enable noImplicitAny
This commit continues where #74 left off by adding parameter and return types to the rest of (non-ecmascript.ts) Temporal source files. Also, it turns on --noImplicitAny. The worst `any` offender is calendar.ts, and fully declaring all its types is more work than I want to take on in this PR, so I did an initial pass on obvious types and assigned a lot of `any` for the rest. Will defnitely need a followup PR to improve calendar.ts. Other than that one file, this commit essentially removes `any` (implicit or otherwise) from the polyfill, with only a few exceptions for truly generic code or testing propeties on `globalThis`. There's still lots of TS work remaining (e.g. `strictNullChecks`, `strictPropertyInitialization`, and cleaning up calendar.ts), but at least we'll now have valid types on all functions. I didn't find any runtime errors as a result of this work (good!) but I did find quite a few problems with the public types in index.d.ts, and this commit fixes those too.
1 parent ac78fd9 commit cc12ab3

19 files changed

+1131
-667
lines changed

.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ rules:
7979
space-infix-ops: error
8080
'@typescript-eslint/explicit-module-boundary-types': off
8181
'@typescript-eslint/no-empty-function': off
82+
'@typescript-eslint/no-empty-interface'
83+
- error
84+
- allowSingleExtends: true
8285
'@typescript-eslint/no-var-requires': off
8386
'@typescript-eslint/ban-ts-comment': off
8487
'@typescript-eslint/no-explicit-any': off # TODO turn this back on

index.d.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ export namespace Temporal {
606606
fields?(fields: Iterable<string>): Iterable<string>;
607607
mergeFields?(fields: Record<string, unknown>, additionalFields: Record<string, unknown>): Record<string, unknown>;
608608
toString(): string;
609+
toJSON?(): string;
609610
}
610611

611612
/**
@@ -683,6 +684,7 @@ export namespace Temporal {
683684
fields(fields: Iterable<string>): Iterable<string>;
684685
mergeFields(fields: Record<string, unknown>, additionalFields: Record<string, unknown>): Record<string, unknown>;
685686
toString(): string;
687+
toJSON(): string;
686688
readonly [Symbol.toStringTag]: 'Temporal.Calendar';
687689
}
688690

@@ -990,14 +992,8 @@ export namespace Temporal {
990992
readonly calendar: Temporal.Calendar;
991993
equals(other: Temporal.PlainTime | PlainTimeLike | string): boolean;
992994
with(timeLike: Temporal.PlainTime | PlainTimeLike, options?: AssignmentOptions): Temporal.PlainTime;
993-
add(
994-
durationLike: Temporal.PlainTime | Temporal.Duration | DurationLike | string,
995-
options?: ArithmeticOptions
996-
): Temporal.PlainTime;
997-
subtract(
998-
durationLike: Temporal.PlainTime | Temporal.Duration | DurationLike | string,
999-
options?: ArithmeticOptions
1000-
): Temporal.PlainTime;
995+
add(durationLike: Temporal.Duration | DurationLike | string, options?: ArithmeticOptions): Temporal.PlainTime;
996+
subtract(durationLike: Temporal.Duration | DurationLike | string, options?: ArithmeticOptions): Temporal.PlainTime;
1001997
until(
1002998
other: Temporal.PlainTime | PlainTimeLike | string,
1003999
options?: DifferenceOptions<'hour' | 'minute' | 'second' | 'millisecond' | 'microsecond' | 'nanosecond'>
@@ -1195,7 +1191,7 @@ export namespace Temporal {
11951191
readonly millisecond: number;
11961192
readonly microsecond: number;
11971193
readonly nanosecond: number;
1198-
readonly timeZone: Temporal.TimeZone;
1194+
readonly timeZone: Temporal.TimeZoneProtocol;
11991195
readonly calendar: CalendarProtocol;
12001196
readonly dayOfWeek: number;
12011197
readonly dayOfYear: number;
@@ -1404,7 +1400,7 @@ export namespace Temporal {
14041400
};
14051401
}
14061402

1407-
declare namespace IntlPolyfill {
1403+
declare namespace Intl {
14081404
type Formattable =
14091405
| Date
14101406
| Temporal.Instant
@@ -1415,11 +1411,11 @@ declare namespace IntlPolyfill {
14151411
| Temporal.PlainYearMonth
14161412
| Temporal.PlainMonthDay;
14171413

1418-
interface DateTimeFormatRangePart extends Intl.DateTimeFormatPart {
1414+
interface DateTimeFormatRangePart extends globalThis.Intl.DateTimeFormatPart {
14191415
source: 'shared' | 'startRange' | 'endRange';
14201416
}
14211417

1422-
export interface DateTimeFormat extends Intl.DateTimeFormat {
1418+
export interface DateTimeFormat extends globalThis.Intl.DateTimeFormat {
14231419
/**
14241420
* Format a date into a string according to the locale and formatting
14251421
* options of this `Intl.DateTimeFormat` object.
@@ -1434,7 +1430,7 @@ declare namespace IntlPolyfill {
14341430
*
14351431
* @param date The date to format.
14361432
*/
1437-
formatToParts(date?: Formattable | number): Intl.DateTimeFormatPart[];
1433+
formatToParts(date?: Formattable | number): globalThis.Intl.DateTimeFormatPart[];
14381434

14391435
/**
14401436
* Format a date range in the most concise way based on the locale and
@@ -1459,23 +1455,31 @@ declare namespace IntlPolyfill {
14591455
formatRangeToParts(startDate: Date | number, endDate: Date | number): DateTimeFormatRangePart[];
14601456
}
14611457

1458+
export interface DateTimeFormatOptions extends Omit<globalThis.Intl.DateTimeFormatOptions, 'timeZone'> {
1459+
timeZone?: string | Temporal.TimeZoneProtocol;
1460+
// TODO: remove the props below after TS lib declarations are updated
1461+
dayPeriod?: 'narrow' | 'short' | 'long';
1462+
dateStyle?: 'full' | 'long' | 'medium' | 'short';
1463+
timeStyle?: 'full' | 'long' | 'medium' | 'short';
1464+
}
1465+
14621466
export const DateTimeFormat: {
14631467
/**
14641468
* Creates `Intl.DateTimeFormat` objects that enable language-sensitive
14651469
* date and time formatting.
14661470
*/
1467-
new (locales?: string | string[], options?: Intl.DateTimeFormatOptions): DateTimeFormat;
1468-
(locales?: string | string[], options?: Intl.DateTimeFormatOptions): DateTimeFormat;
1471+
new (locales?: string | string[], options?: DateTimeFormatOptions): DateTimeFormat;
1472+
(locales?: string | string[], options?: DateTimeFormatOptions): DateTimeFormat;
14691473

14701474
/**
14711475
* Get an array containing those of the provided locales that are supported
14721476
* in date and time formatting without having to fall back to the runtime's
14731477
* default locale.
14741478
*/
1475-
supportedLocalesOf(locales: string | string[], options?: Intl.DateTimeFormatOptions): string[];
1479+
supportedLocalesOf(locales: string | string[], options?: DateTimeFormatOptions): string[];
14761480
};
14771481
}
14781482

1479-
export { IntlPolyfill as Intl };
1483+
export { Intl as Intl };
14801484

14811485
export function toTemporalInstant(this: Date): Temporal.Instant;

0 commit comments

Comments
 (0)