This code works in JavaScript:
var timeFormat = d3.timeFormat("%M:%S");
var yAxis = d3.axisLeft(y).tickFormat(timeFormat)
But this code in TypeScript does not work:
const yAxis = d3.axisLeft(y).tickFormat(d3.timeFormat("%M:%S"));
function timeFormat(specifier: string): (date: Date) => string Returns a new formatter for the given string specifier. The returned function formats a specified date, returning the corresponding string.
An alias for locale.format (TimeLocaleObject.format) on the default locale.
@param specifier — A specifier string for the date format.
The error is
Argument of type '(date: Date) => string' is not assignable to parameter of type 'null'.ts(2345)
There's a much simpler modern solution
const yAxis = d3.axisLeft<Date>(y).tickFormat(d3.timeFormat("%M:%S"));
works with d3v4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With