Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript error when using d3.timeFormat in axis.tickFormat()

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)

like image 277
gpbaculio Avatar asked May 05 '19 14:05

gpbaculio


1 Answers

There's a much simpler modern solution

const yAxis = d3.axisLeft<Date>(y).tickFormat(d3.timeFormat("%M:%S"));

works with d3v4

like image 64
cjds Avatar answered Oct 05 '22 03:10

cjds