c# - Typescript with d3js with definitlyTyped -
so, first day typescript.
i ran trouble code:
var x: d3.scale.ordinal = d3.scale.ordinal() .rangeroundbands([0, +this.params.width], .1) .domain(data.map(function (d) { return d.dayname; }));
i want x of type ordinal scale, reason error:
generic type 'ordinal<domain, range>' requires 2 type argument(s).
and sure enough in code definitlytyped can see following :
interface ordinal<domain extends { tostring(): string }, range> { (x: domain): range; domain(): domain[]; domain(values: domain[]): ordinal<domain, range>; range(): range[]; range(values: range[]): ordinal<domain, range>; rangepoints(interval: [number, number], padding?: number): ordinal<domain, number>; rangeroundpoints(interval: [number, number], padding?: number): ordinal<domain, number>; rangebands(interval: [number, number], padding?: number, outerpadding?: number): ordinal<domain, number>; rangeroundbands(interval: [number, number], padding?: number, outerpadding?: number): ordinal<domain, number>; rangeband(): number; rangeextent(): [number, number]; copy(): ordinal<domain, range>; }
so ordinal need have 2 types, domain , range, cant find anywhere structure.
how solve problem? or have misunderstood something?
domain , range generic type arguments, placeholders types need provide rather actual concrete types. domain can type has tostring method, , range can apparently type want.
typings in definitelytyped repository have tests ensure typings work expected, , if you're trying figure out how use typings first step looking @ tests. here's relevant example d3 tests:
var fill = d3.scale.ordinal<number, string>() .domain(d3.range(4)) .range(["#000000", "#ffdd89", "#957244", "#f26223"]);
so think should work you:
var x: d3.scale.ordinal<string, number> = d3.scale.ordinal<string, number>() .rangeroundbands([0, +this.params.width], .1) .domain(data.map(function (d) { return d.dayname; }));
or can let compiler deduce type of variable:
var x = d3.scale.ordinal<string, number>() .rangeroundbands([0, +this.params.width], .1) .domain(data.map(function (d) { return d.dayname; }));
if you're unfamiliar generic types in typescript, suggest reading section on generics in typescript handbook.
Comments
Post a Comment