Skip to content

Types - #17

Draft
daogrady wants to merge 23 commits into
mainfrom
feat/types
Draft

Types#17
daogrady wants to merge 23 commits into
mainfrom
feat/types

Conversation

@daogrady

@daogrady daogrady commented Mar 17, 2025

Copy link
Copy Markdown

Adds type definitions.
Also, some rewrites were required. I tried to rewrite as sparingly as possible. See comments. If you see any rewrite that changes the behaviour in an unacceptable way, please let me know.

Comment thread lib/expect.js
)

const expect = module.exports = actual => {
const chainable = function (x) { return this.call(x) }; delete chainable.length

@daogrady daogrady Mar 27, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Rewrite

This caused

The operand of a 'delete' operator cannot be a read-only property.ts(2704)

And it does not seem to have any effect anyway:

Screenshot 2025-03-27 at 14 09 46

Comment thread lib/axios.js Outdated
axios.defaults.baseURL = url
axios.default.defaults.baseURL = url
}})
return super.axios = axios

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Rewrite

to my understanding, this super.… = … technique was used to have a truly private variable. I replaced them with actual private variables.

Comment thread lib/cds-test.js
* @param {(message?: any, ...optionalParams: any[]) => void} capture
*/
log (_capture) {
log (capture) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Renamed, as underscore is generally used for "variables I don't use", "don't care", or "throwaway" (both, in Python, as well as in the ESLint rules no-unused-vars).
If there's a good reason for keeping the underscore, do let me know.

Comment thread lib/fixtures/test-each.js
*/
return (msg,fn) => Promise.all (table.map (each => {
const args = Array.isArray(each) ? each : [each], [label] = args
return this (format(msg, label), ()=> fn(...args))

@daogrady daogrady Sep 11, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

not entirely sure what is supposed to happen here. this refers to module, which is not callable.
I guess this is supposed to be return each (...)? (If we intend to fix this, be aware that each (the outer function) is shadowed by each (the parameter of the inner function), so some renaming or aliasing needs to take place. Or use this.exports(...).

Comment thread lib/fixtures/node-test.js
return require.cache[path] = o?.virtual ? fn() : Object.assign (require(path), fn())
}}
}
return undefined

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this happened implicitly before and TS didn't like it that way.

Comment thread lib/expect.js
get eq() { return this.equals }
get eql() { return this.eqls }
// @ts-expect-error - FIXME! This is probably actually missing!
get exists() { return this.defined }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I can not find defined in the class hierarchy and TS is also complaining about it missing. Unless this is injected in some voodoo way (please don't...) this is probably actually missing and should be amended.

Comment thread lib/expect.js
if (is.array(a)) return a.includes(x) || this._deep && a.some(o => compare(o,x))
if (is.set(a)) return a.has(x)
if (is.object(a)) return compare(a,x)
return false

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this turns the return type from boolean | undefined into boolean. I assume that we used the implicit undefined as falsy value, not to have any explicit === undefined comparison. If we did rely on this ternary return type, let me know.

Comment thread lib/expect.js
get true() { return this.assert(a => a === true) || this.should`be ${true}` }
get false() { return this.assert(a => a === false) || this.should`be ${false}` }
get empty() { return this.assert(a => !a?.length === 0 || Object.keys(a).length === 0) || this.should`be empty` }
// @ts-expect-error - FIXME: this is always false due to precedence!

@daogrady daogrady Sep 11, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

!a?.length is coerced into boolean, followed in a strict comparison between boolean and 0, which always yields false.
Screenshot 2025-09-11 at 16 49 10

Comment thread global.d.ts
function afterEach(): void;
function afterAll(method: Function): void;
function afterAll(message: string & Function?, method: Function): void;
function expect(_:any): void;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

what is the signature for this parameter? I derived it from node-test.js where it is used as expect(this), but no idea what the proper type should be there.

Comment thread lib/naxios.js
*/
function (url, config) {
if (new.target) return new Naxios (url)
else config = typeof url === 'object' ? url : { url, ...config }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

url appears to be an object only in some cases. I assume in other cases it can be a string (I therefore added the above type). The constructor call would then attempt to destructure a string. While that "works":

> var x = {...'str'}
undefined
> x
{ '0': 's', '1': 't', '2': 'r' }

I guess that is not really intended behaviour?

@daogrady
daogrady requested review from chgeo and danjoa September 22, 2025 11:11
@daogrady

Copy link
Copy Markdown
Author

@chgeo as discussed, I'll temporarily leave the types as is. There are still various errors to address, but I think it makes sense to resolve the open discussion points (see my comments on this PR and FIXMEs in code) first.

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.

1 participant