Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeScript errors in lib/utils #231

Open
wants to merge 5 commits into
base: ts-fixes
Choose a base branch
from

Conversation

rgieseke
Copy link
Contributor

This fixes a few more TypeScript errors, those in lib/utils (see #218).

The error messages were:

src/lib/utils/makeAccessor.js:4:33
Error: Generic type 'Array<T>' requires 1 type argument(s). 
	Make an accessor from a string, number, function or an array of the combination of any
	@param {string|number|Function|Array} acc The accessor function, key or list of them.
	@returns {Function} An accessor function.
--
src/lib/utils/makeAccessor.js:8:23
Error: Type 'null' is not assignable to type 'Function'. 
export default function makeAccessor(acc) {
	if (!canBeZero(acc)) return null;
	if (Array.isArray(acc)) {
--
src/lib/utils/makeAccessor.js:10:10
Error: Parameter 'd' implicitly has an 'any' type. 
	if (Array.isArray(acc)) {
		return d =>
			acc.map(k => {
--
src/lib/utils/makeAccessor.js:15:10
Error: Parameter 'd' implicitly has an 'any' type. 
	} else if (typeof acc !== 'function') {
		return d => d[acc];
	}
--
src/lib/utils/filterObject.js:10:34
Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'. 
		Object.entries(obj).filter(([key, value]) => {
			return value !== undefined && comparisonObj[key] === undefined;
--
src/lib/utils/debounce.js:8:6
Error: Variable 'timer' implicitly has type 'any' in some locations where its type cannot be determined. 
export default function debounce(func, timeout = 300) {
	let timer;
	return (...args) => {
--
src/lib/utils/debounce.js:9:10
Error: Rest parameter 'args' implicitly has an 'any[]' type. 
	let timer;
	return (...args) => {
		clearTimeout(timer);
--
src/lib/utils/debounce.js:10:16
Error: Variable 'timer' implicitly has an 'any' type. 
	return (...args) => {
		clearTimeout(timer);
		timer = setTimeout(() => {
--
src/lib/utils/debounce.js:12:15
Error: 'this' implicitly has type 'any' because it does not have a type annotation. 
		timer = setTimeout(() => {
			func.apply(this, args);
		}, timeout);
--
src/lib/utils/arraysEqual.js:6:10
Error: Generic type 'Array<T>' requires 1 type argument(s). 
	of making a set
	@param {Array} arr1 An array to test
	@param {Array} arr2 An array to test against
--
src/lib/utils/arraysEqual.js:7:10
Error: Generic type 'Array<T>' requires 1 type argument(s). 
	@param {Array} arr1 An array to test
	@param {Array} arr2 An array to test against
	@returns {boolean} Whether they contain all and only the same items
--
src/lib/utils/padScale.js:19:34
Error: Parameter 'scale' implicitly has an 'any' type. 

export default function padScale(scale, padding) {
	if (typeof scale.range !== 'function') {
--
src/lib/utils/padScale.js:19:41
Error: Parameter 'padding' implicitly has an 'any' type. 

export default function padScale(scale, padding) {
	if (typeof scale.range !== 'function') {
--
src/lib/utils/padScale.js:41:38
Error: Parameter 'd' implicitly has an 'any' type. 

	const [d1, d2] = scale.domain().map(d => {
		return isTime ? lift(d.getTime()) : lift(d);

@rgieseke rgieseke force-pushed the ts-fixes-lib-utils branch from 23a250c to 39e9c1f Compare August 29, 2024 09:11
const unpaddable = ['scaleThreshold', 'scaleQuantile', 'scaleQuantize', 'scaleSequentialQuantile'];

/**
* @typedef {import('d3-scale').ScaleLinear<any, any> |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I went through the d3-docs and added the interface definitions.

Not sure whether one should exclude those that are filtered out with unpaddable or whether there is already combinations like scaleContinuousNumeric which would make the list shorter.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, even though the script will handle an unpaddable scale, that it should be a typescript error if you try to pass in an unpaddable scale.

I didn't follow the second part of what you were saying about scaleContinuous Numeric. Can you explain what you mean more?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have linked that. in d3-typed there is scaleContinousNumeric, which then gets extended to ScaleLinear, ScalePower, ScaleLogarithmic and a few more.

@mhkeller
Copy link
Owner

Thanks for this one. I was away for a few weeks and will go through this.

src/lib/utils/debounce.js Outdated Show resolved Hide resolved
src/lib/utils/padScale.js Outdated Show resolved Hide resolved
@mhkeller
Copy link
Owner

mhkeller commented Oct 7, 2024

I think this is looking really good. I made some changes and added some comments. I still need to go through makeAccessor. It may be an improvement to define the Function more.

@param {Function} scale A D3 scale funcion
@param {number[]} padding A two-value array of numbers specifying padding in pixels
@param {Scale} scale A D3 scale funcion
@param {[number, number]} padding A two-value array of numbers specifying padding in pixels
@returns {number[]} The padded domain
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type is [number, number] also, right?

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.

2 participants