Skip to content

[d3] Correct interpolate() signature - #5095

Merged
borisyankov merged 2 commits into
DefinitelyTyped:masterfrom
aicioara:master
Jul 30, 2015
Merged

borisyankov merged 2 commits into
DefinitelyTyped:masterfrom
aicioara:master

Conversation

@aicioara

Copy link
Copy Markdown
Contributor

This code would not have compiled before

/// <reference path="d3.d.ts" />

var line1 = d3.svg.line().interpolate("step");
var line2 = d3.svg.line().interpolate(line1.interpolate());

It compiles now.

The reason for that is described here
microsoft/TypeScript#1805

In short, since the return type of interpolate is the union string | ((points: Array<[number, number]>) => string), that is what the argument should look like as well. Splitting it into overloaded methods does not work until TS#1805 is closed

@vvakame

vvakame commented Jul 28, 2015

Copy link
Copy Markdown
Member

d3/d3.d.ts

to authors(@gustavderdrache @borisyankov). could you review this PR?
👍 or 👎?

check list

  • pass the Travic-CI test?

@gustavderdrache

Copy link
Copy Markdown
Contributor

Boy, this really is obnoxious. I feel pretty strongly that this is at least partly related to d3 not actually upcasting values when it arguably should be.

The compiler bug also affects line.x, so I'm going to use that for demonstration. Looking at the line() function itself, we see d3 doing d3.functor(x) when the line is drawn, as opposed to when x is set. As a result,

typeof d3.svg.line().x(5).x() === 'number'
typeof d3.svg.line().x(d3.functor(5)).x() === 'function'

when really both should be upcast by d3.functor when set. This is the behavior of symbol.type, which is why the accessor has a non-union return type.

As far as fixing this goes, it's worth taking a look at other setter/getter patterns that are affected by this bug (at least other SVG generators like d3.svg.area). It may even be worth opening PRs against D3 itself and standardizing the behavior.

@aicioara

Copy link
Copy Markdown
Contributor Author

@gustavderdrache @borisyankov What is the status of this? I would like if we could merge this in, then go for a more general fix (i.e. fix it everywhere else this happens)

@gustavderdrache

Copy link
Copy Markdown
Contributor

👍

I'd rather have the full fix (along with unit tests), but this is a step in the right direction.

borisyankov added a commit that referenced this pull request Jul 30, 2015
[d3] Correct interpolate() signature
@borisyankov
borisyankov merged commit 383a3bc into DefinitelyTyped:master Jul 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants