d3-axis strictNullChecks and strictFunctionTypes (#25066)

This commit is contained in:
denisname
2018-04-24 16:06:06 -07:00
committed by Wesley Wigham
parent 3363dfb20e
commit 0c0e3799ec
3 changed files with 48 additions and 28 deletions
+19 -7
View File
@@ -47,6 +47,7 @@ axisScaleNumber = scaleBand<number>();
axisScaleNumber = scalePoint<number>();
axisScaleString = scaleBand();
axisScaleString = scalePoint();
// --------------------------------------------------------------------------
// Test AxisContainerElement
// --------------------------------------------------------------------------
@@ -58,7 +59,8 @@ const canvas: HTMLCanvasElement = select<HTMLCanvasElement, any>('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<number | { valueOf(): number }> = d3Axis.axisLeft(scal
leftAxis = leftAxis.scale(scalePow());
const powerScale: ScalePower<number, number> = leftAxis.scale<ScalePower<number, number>>();
// powerScale = leftAxis.scale(); // fails, without casting as AxisScale is purposely generic
bottomAxis = bottomAxis.scale(scaleOrdinal<number>());
// 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<string> = bottomAxis.scale();
const ordinalScale: ScaleOrdinal<string, number> = bottomAxis.scale<ScaleOrdinal<string, number>>();
// 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<SVGSVGElement, any, any, any> = select<SVGSVGElement, any>('g');
const svgSelection: Selection<SVGSVGElement, any, any, any> = select<SVGSVGElement, any>('svg');
const svgTransition = svgSelection.transition();
svgSelection.call(leftAxis);
svgTransition.call(leftAxis);
const pathSelection: Selection<SVGPathElement, any, any, any> = select<SVGPathElement, any>('path');
const pathTransition = svgSelection.transition();
// // $ExpectError
// pathSelection.call(bottomAxis);
// // $ExpectError
// pathSelection.call(bottomAxis);
const canvasSelection: Selection<HTMLCanvasElement, any, any, any> = select<HTMLCanvasElement, any>('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
+28 -20
View File
@@ -1,6 +1,9 @@
// Type definitions for D3JS d3-axis module 1.0
// Project: https://github.com/d3/d3-axis/
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>, Alex Ford <https://github.com/gustavderdrache>, Boris Yankov <https://github.com/borisyankov>
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
// Alex Ford <https://github.com/gustavderdrache>
// Boris Yankov <https://github.com/borisyankov>
// denisname <https://github.com/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<Domain> {
(x: Domain): number | undefined;
@@ -37,7 +45,7 @@ export interface AxisScale<Domain> {
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<Domain> {
}
/**
* 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 <Domain> is the type of the axis domain
* Interface defining an axis generator. The generic <Domain> is the type of the axis domain.
*/
export interface Axis<Domain> {
/**
@@ -58,14 +66,14 @@ export interface Axis<Domain> {
*
* @param context A selection of SVG containers (either SVG or G elements).
*/
(context: Selection<AxisContainerElement, any, any, any>): void;
(context: Selection<SVGSVGElement, any, any, any> | Selection<SVGGElement, any, any, any>): void;
/**
* Render the axis to the given context.
*
* @param context A transition defined on SVG containers (either SVG or G elements).
*/
(context: TransitionLike<AxisContainerElement, any>): void;
(context: TransitionLike<SVGSVGElement, any> | TransitionLike<SVGGElement, any>): void;
/**
* Gets the current scale underlying the axis.
@@ -75,7 +83,7 @@ export interface Axis<Domain> {
/**
* 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<Domain>): this;
@@ -86,7 +94,7 @@ export interface Axis<Domain> {
*
* 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<Domain> {
*
* 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<Domain> {
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<Domain> {
/**
* 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<Domain> {
* 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<Domain>(scale: AxisScale<Domain>): Axis<Domain>;
export function axisTop<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
/**
* 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<Domain>(scale: AxisScale<Domain>): Axis<Domain>;
export function axisRight<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
/**
* 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<Domain>(scale: AxisScale<Domain>): Axis<Domain>;
export function axisBottom<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
/**
* 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<Domain>(scale: AxisScale<Domain>): Axis<Domain>;
export function axisLeft<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
+1 -1
View File
@@ -8,7 +8,7 @@
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": false,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"