TypeScript: Missing specific types in DateTimeFormatOptions
TypeScript Version: 3.7.3
Search Terms: Intl.DateTimeFormat, DateTimeFormatOptions, date format types
Code
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp,
(See below)
DateTimeFormatOptions interface missing specific types. https://github.com/microsoft/TypeScript/blob/408b17649a1197a52f68fcb49b8c2f1eeac13668/src/lib/es5.d.ts#L4253-L4267
Expected behavior: I would expect the types to match what’s listed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
interface DateTimeFormatOptions {
localeMatcher?: 'lookup' | 'best fit';
weekday?: 'long' | 'short' | 'narrow';
era?: 'long' | 'short' | 'narrow';
year?: 'numeric' | '2-digit';
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
day?: 'numeric' | '2-digit';
hour?: 'numeric' | '2-digit';
minute?: 'numeric' | '2-digit';
second?: 'numeric' | '2-digit';
timeZoneName?: 'long' | 'short';
formatMatcher?: 'basic' | 'best fit';
hour12?: boolean;
timeZone?: string; // this is more complicated than the others, not sure what I expect here
}
Actual behavior:
interface DateTimeFormatOptions {
localeMatcher?: string;
weekday?: string;
era?: string;
year?: string;
month?: string;
day?: string;
hour?: string;
minute?: string;
second?: string;
timeZoneName?: string;
formatMatcher?: string;
hour12?: boolean;
timeZone?: string;
}
Playground Link:
Related Issues: Couldn’t find any
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 34
- Comments: 19 (3 by maintainers)
Commits related to this issue
- fix: fixes #35865 - document and flesh out legal Intl.DateTimeFormatOptions types — committed to johan/TypeScript by johan 4 years ago
- fix: fixes #35865 - document and flesh out legal Intl.DateTimeFormatOptions types — committed to johan/TypeScript by johan 4 years ago
- fix: fixes #35865 - document and flesh out legal Intl.DateTimeFormatOptions types — committed to johan/TypeScript by johan 4 years ago
- fix: fixes #35865 - document and flesh out legal Intl.DateTimeFormatOptions types — committed to johan/TypeScript by johan 4 years ago
- feat: several fixes and new features — committed to Planning-nl/vue3-i18n by RobbinBaauw 4 years ago
- fixing typing issue caused by https://github.com/microsoft/TypeScript/issues/35865 — committed to bullhorn/novo-elements by dvoegelin 2 years ago
- refactor!: novo-elements 7 release including TS4 and NG13 updates (#1274) * refactor(): some remaining prep work for the upcoming typescript 4 upgrade * adding value getters to some table cells ... — committed to bullhorn/novo-elements by dvoegelin 2 years ago
- feat(QueryBuilder): New QueryBuilder component for dynamically generating queries (#1328) * Select is intercepting clicks to the ng-content area * adding circle icon shape * not closing overlay... — committed to bullhorn/novo-elements by deleted user 2 years ago
- feat(): next (#1336) * feat(Autocomplete): Autocomplete now works with ChipList (#1326) * fix(Chips): update the ux for disabled chips (#1331) -darken the opacity to improve readability -updat... — committed to bullhorn/novo-elements by dvoegelin 2 years ago
- feat(QueryBuilder): New QueryBuilder component for dynamically generating queries (#1328) * Select is intercepting clicks to the ng-content area * adding circle icon shape * not closing overlay if ... — committed to bullhorn/novo-elements by deleted user 2 years ago
Running into this issue for
dateStyleandtimeStyleoptions as well.Until this gets fixed you can extend the TypeScript definition for
DateTimeFormatOptionsby writing the following code in your project’s rootindex.d.tsfileI wonder if these are even related. Should we create a separate issue for that? Because those two properties are just missing entirely.
We don’t like to replace basic TS declares unless necessary, so we extend the base. For that reason I just created a complete type from the MDN docs.
I’d love to see this discussed, or move forward. To me, moving runtime exceptions out of js into hard compile-time errors in typescript is one of the strongest points of typescript to begin with.
Here’s a breaking change:
The author of
makeOptionswill have to propagate the stricter type offormatMatcherout, possibly breaking its consumers.It appears to be available if you’re targeting ES2020. The TS Playground demo passes type checking if I change the target to ES2020.
This issue can be closed. As of TS 4.2 these types on
Intl.DateTimeFormatOptionsuse unions of strings where possible per;https://github.com/microsoft/TypeScript/blob/v4.3.5/src/lib/es5.d.ts#L4332-L4346 and https://github.com/microsoft/TypeScript/blob/v4.3.5/src/lib/es2020.intl.d.ts#L280-L288
Note that you will need to target es2020 or otherwise add it to your
libfor the properties defined ines2020.intl.d.tsto be presentI don’t have any other dependencies in my project. I’m only using VS Code and setting it to use typescript
v4.2.3.Would be a breaking change; we’ll have to discuss.