diff --git a/types/d3-axis/d3-axis-tests.ts b/types/d3-axis/d3-axis-tests.ts index f2b47beaa8..8edd1ab1c4 100644 --- a/types/d3-axis/d3-axis-tests.ts +++ b/types/d3-axis/d3-axis-tests.ts @@ -47,6 +47,7 @@ axisScaleNumber = scaleBand(); axisScaleNumber = scalePoint(); axisScaleString = scaleBand(); axisScaleString = scalePoint(); + // -------------------------------------------------------------------------- // Test AxisContainerElement // -------------------------------------------------------------------------- @@ -58,7 +59,8 @@ const canvas: HTMLCanvasElement = select('canvas').node( containerElement = svg; containerElement = g; -// containerElement = canvas; // fails, incompatible type +// $ExpectError +containerElement = canvas; // fails, incompatible type // -------------------------------------------------------------------------- // Test Axis Generators @@ -77,14 +79,13 @@ let leftAxis: d3Axis.Axis = d3Axis.axisLeft(scal leftAxis = leftAxis.scale(scalePow()); const powerScale: ScalePower = leftAxis.scale>(); -// powerScale = leftAxis.scale(); // fails, without casting as AxisScale is purposely generic bottomAxis = bottomAxis.scale(scaleOrdinal()); -// bottomAxis = bottomAxis.scale(scalePow()) // fails, domain of scale incompatible with domain of axis +// $ExpectError +bottomAxis = bottomAxis.scale(scalePow()); // fails, domain of scale incompatible with domain of axis const axisScale: d3Axis.AxisScale = bottomAxis.scale(); const ordinalScale: ScaleOrdinal = bottomAxis.scale>(); -// ordinalScale = bottomAxis.scale(); // fails, without casting as AxisScale is purposely generic // ticks(...) ---------------------------------------------------------------- @@ -119,6 +120,7 @@ const formatFn: ((domainValue: string, index: number) => string) | null = bottom bottomAxis.tickFormat((d, i) => '#' + i); bottomAxis.tickFormat(d => d + '!'); + // tickSize(...) ---------------------------------------------------------------- rightAxis = rightAxis.tickSize(5); @@ -149,14 +151,24 @@ const gTransition = gSelection.transition(); gSelection.call(topAxis); gTransition.call(topAxis); -const svgSelection: Selection = select('g'); +const svgSelection: Selection = select('svg'); const svgTransition = svgSelection.transition(); svgSelection.call(leftAxis); svgTransition.call(leftAxis); +const pathSelection: Selection = select('path'); +const pathTransition = svgSelection.transition(); + +// // $ExpectError +// pathSelection.call(bottomAxis); +// // $ExpectError +// pathSelection.call(bottomAxis); + const canvasSelection: Selection = select('canvas'); const canvasTransition = canvasSelection.transition(); -// canvasSelection.call(rightAxis); // fails, incompatible context container element -// canvasTransition.call(rightAxis); // fails, incompatible context container element +// $ExpectError +canvasSelection.call(rightAxis); // fails, incompatible context container element +// $ExpectError +canvasTransition.call(rightAxis); // fails, incompatible context container element diff --git a/types/d3-axis/index.d.ts b/types/d3-axis/index.d.ts index 1c7820c809..ea67b74209 100644 --- a/types/d3-axis/index.d.ts +++ b/types/d3-axis/index.d.ts @@ -1,6 +1,9 @@ // Type definitions for D3JS d3-axis module 1.0 // Project: https://github.com/d3/d3-axis/ -// Definitions by: Tom Wanzek , Alex Ford , Boris Yankov +// Definitions by: Tom Wanzek +// Alex Ford +// Boris Yankov +// denisname // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // Last module patch version validated against: 1.0.8 @@ -11,6 +14,11 @@ import { Selection, TransitionLike } from 'd3-selection'; // Shared Types and Interfaces // -------------------------------------------------------------------------- +/** + * A helper type to alias elements which can serve as a domain for an axis. + */ +export type AxisDomain = number | string | Date | { valueOf(): number}; + /** * A helper interface to describe the minimal contract to be met by a time interval * which can be passed into the Axis.ticks(...) or Axis.tickArguments(...) methods when @@ -29,7 +37,7 @@ export interface AxisTimeInterval { /** * A helper interface to which a scale passed into axis must conform (at a minimum) - * for axis to use the scale without error + * for axis to use the scale without error. */ export interface AxisScale { (x: Domain): number | undefined; @@ -37,7 +45,7 @@ export interface AxisScale { range(): number[]; copy(): this; bandwidth?(): number; - // TODO: Reconsider the below, note that the compiler does not differentiate the overloads w.r.t. optionality + // TODO: Reconsider the below, note that the compiler does not differentiate the overloads w.r.t. optionality // ticks?(count?: number): Domain[]; // ticks?(count?: AxisTimeInterval): Date[]; // tickFormat?(count?: number, specifier?: string): ((d: number) => string); @@ -45,12 +53,12 @@ export interface AxisScale { } /** - * A helper type to alias elements which can serve as a container for an axis + * A helper type to alias elements which can serve as a container for an axis. */ export type AxisContainerElement = SVGSVGElement | SVGGElement; /** - * Interface defining an axis generator. The generic is the type of the axis domain + * Interface defining an axis generator. The generic is the type of the axis domain. */ export interface Axis { /** @@ -58,14 +66,14 @@ export interface Axis { * * @param context A selection of SVG containers (either SVG or G elements). */ - (context: Selection): void; + (context: Selection | Selection): void; /** * Render the axis to the given context. * * @param context A transition defined on SVG containers (either SVG or G elements). */ - (context: TransitionLike): void; + (context: TransitionLike | TransitionLike): void; /** * Gets the current scale underlying the axis. @@ -75,7 +83,7 @@ export interface Axis { /** * Sets the scale and returns the axis. * - * @param scale The scale to be used for axis generation + * @param scale The scale to be used for axis generation. */ scale(scale: AxisScale): this; @@ -86,7 +94,7 @@ export interface Axis { * * This method is also a convenience function for axis.tickArguments. * - * @param count Number of ticks that should be rendered + * @param count Number of ticks that should be rendered. * @param specifier An optional format specifier to customize how the tick values are formatted. */ ticks(count: number, specifier?: string): this; @@ -178,7 +186,7 @@ export interface Axis { * * See also axis.ticks. * - * @param args An array with arguments suitable for the scale to be used for tick generation + * @param args An array with arguments suitable for the scale to be used for tick generation. */ tickArguments(args: any[]): this; @@ -210,7 +218,7 @@ export interface Axis { tickFormat(): ((domainValue: Domain, index: number) => string) | null; /** - * Sets the tick format function and returns the axis. + * Sets the tick format function and returns the axis. * * @param format A function mapping a value from the axis Domain to a formatted string * for display purposes. When invoked, the format function is also passed a second argument representing the zero-based index @@ -287,7 +295,7 @@ export interface Axis { /** * Set the current padding and return the axis. * - * @param padding Padding in pixels (Default is 3). + * @param padding Padding in pixels (Default is 3). */ tickPadding(padding: number): this; } @@ -296,30 +304,30 @@ export interface Axis { * Constructs a new top-oriented axis generator for the given scale, with empty tick arguments, * a tick size of 6 and padding of 3. In this orientation, ticks are drawn above the horizontal domain path. * - * @param scale The scale to be used for axis generation + * @param scale The scale to be used for axis generation. */ -export function axisTop(scale: AxisScale): Axis; +export function axisTop(scale: AxisScale): Axis; /** * Constructs a new right-oriented axis generator for the given scale, with empty tick arguments, * a tick size of 6 and padding of 3. In this orientation, ticks are drawn to the right of the vertical domain path. * - * @param scale The scale to be used for axis generation + * @param scale The scale to be used for axis generation. */ -export function axisRight(scale: AxisScale): Axis; +export function axisRight(scale: AxisScale): Axis; /** * Constructs a new bottom-oriented axis generator for the given scale, with empty tick arguments, * a tick size of 6 and padding of 3. In this orientation, ticks are drawn below the horizontal domain path. * - * @param scale The scale to be used for axis generation + * @param scale The scale to be used for axis generation. */ -export function axisBottom(scale: AxisScale): Axis; +export function axisBottom(scale: AxisScale): Axis; /** * Constructs a new left-oriented axis generator for the given scale, with empty tick arguments, * a tick size of 6 and padding of 3. In this orientation, ticks are drawn to the left of the vertical domain path. * - * @param scale The scale to be used for axis generation + * @param scale The scale to be used for axis generation. */ -export function axisLeft(scale: AxisScale): Axis; +export function axisLeft(scale: AxisScale): Axis; diff --git a/types/d3-axis/tsconfig.json b/types/d3-axis/tsconfig.json index 5b54d1f622..0e15092e48 100644 --- a/types/d3-axis/tsconfig.json +++ b/types/d3-axis/tsconfig.json @@ -8,7 +8,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, - "strictFunctionTypes": false, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../"