Skip to content

hourCycle option not recognized for Date.toLocaleString #34399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MasterOdin opened this issue Oct 14, 2019 · 1 comment
Closed

hourCycle option not recognized for Date.toLocaleString #34399

MasterOdin opened this issue Oct 14, 2019 · 1 comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript
Milestone

Comments

@MasterOdin
Copy link

MasterOdin commented Oct 14, 2019

TypeScript fails to recognize the hourCycle parameter for toLocaleString, documented on MDN.

TypeScript Version: 3.7.0-dev.20191014

Search Terms: toLocaleString, hourCycle

Code: TS2345

const a = (new Date()).toLocaleString('en-US', {hourCycle: 'h24'});
console.log(a);

Expected behavior:
Compiles without error

Actual behavior:

test.ts:1:51 - error TS2345: Argument of type '{ hourCycle: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Object literal may only specify known properties, and 'hourCycle' does not exist in type 'DateTimeFormatOptions'.

1 const a = (new Date()).toLocaleString('en-US', {hourCycle: 'h24'});
                                                  ~~~~~~~~~~~~~~~~


Found 1 error.

The generated code then runs fine under Node 12 (I think it's not supported below this version) and in browsers and prints the expected output of the date with requested hour cycle.

Playground Link: http://www.typescriptlang.org/play/?ts=Nightly&ssl=1&ssc=50&pln=1&pc=70#code/MYewdgzgLgBAhjAvDAFGApgdxgETldFASiIDooQAZEYOAG3QGUoAnASzAHMUBXMAE3QAzDun4AaGAG8YACxA8WAYQCewBgC4YAclkBGAEzaYAXyIBuIA

Related Issues:
I did not find any.

@BPScott
Copy link

BPScott commented Aug 12, 2021

This issue can be closed.

As of TypeScript 4.2 hourCycle is present on DateTimeFormatOptions - the interface used for the second parameter of toLocaleString - when you target es2020 or include it in your lib.

This is a TS playground demonstrating that no errors occur if you target es2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants