Skip to content

Add TypeScript type definitions#97

Open
oyzamil wants to merge 3 commits into
uzairfarooq:masterfrom
oyzamil:add-types
Open

Add TypeScript type definitions#97
oyzamil wants to merge 3 commits into
uzairfarooq:masterfrom
oyzamil:add-types

Conversation

@oyzamil

@oyzamil oyzamil commented Dec 24, 2025

Copy link
Copy Markdown

What this PR does

  • Adds TypeScript declarations
  • No runtime changes

Why

This package does not currently provide TypeScript typings, which causes
any types for TS users.

@uzairfarooq

Copy link
Copy Markdown
Owner

Code review

Found 4 issues:

  1. Options interface is missing the timeout property. The source declares timeout: 0 as a default in both arriveDefaultOptions and leaveDefaultOptions, reads options.timeout in addTimeoutHandler, and the README documents this option. TypeScript users passing { timeout: 5000 } will get a type error.

arrive/index.d.ts

Lines 2 to 8 in a420e9e

interface Options {
fireOnAttributesModification?: boolean | undefined;
onceOnly?: boolean | undefined;
existing?: boolean | undefined;
}

(see source default at

arrive/src/arrive.js

Lines 300 to 306 in a420e9e

// Default options for 'arrive' event
var arriveDefaultOptions = {
fireOnAttributesModification: false,
onceOnly: false,
existing: false,
timeout: 0 // default 0 (no timeout)
};
)

  1. ArriveSignature and LeaveSignature return void, but both functions return Promise<Element> when called without a callback (async/await support). README documents var newElem = await document.arrive(".test-elem"). Return type should be void | Promise<Element>; otherwise the documented promise-based usage will not type-check.

arrive/index.d.ts

Lines 8 to 24 in a420e9e

type ArriveSignature = (
element: string,
handlerOrOptions: ((this: Element, element: Element) => void) | Options,
handler?: (this: Element, element: Element) => void,
) => void;
type UnbindArriveSignature = (
elementOrHandler?: string | ((this: Element, element: Element) => void),
handler?: (this: Element, element: Element) => void,
) => void;
type LeaveSignature = (
element: string,
handlerOrOptions: ((this: Element, element: Element) => void) | Options,
handler?: (this: Element) => void,
) => void;
type UnbindLeaveSignature = (
elementOrHandler?: string | ((this: Element, element: Element) => void),

(returns at

arrive/src/arrive.js

Lines 388 to 402 in a420e9e

return callback.call(existing[0].elem, existing[0].elem);
} else {
return Promise.resolve(existing[0].elem);
}
}
setTimeout(utils.callCallbacks, 1, existing);
}
if (callback) {
mutationBindEvent.call(this, selector, options, callback);
} else {
var a = this;
return new Promise(resolve => mutationBindEvent.call(a, selector, options, resolve));
}
)

  1. ArriveSignature and LeaveSignature require handlerOrOptions as the second argument (no ?). The implementation treats both arg2 and arg3 as optional, and the README's promise-only call await document.arrive(".test-elem") passes only the selector. handlerOrOptions should be optional.

arrive/index.d.ts

Lines 9 to 23 in a420e9e

type ArriveSignature = (
element: string,
handlerOrOptions: ((this: Element, element: Element) => void) | Options,
handler?: (this: Element, element: Element) => void,
) => void;
type UnbindArriveSignature = (
elementOrHandler?: string | ((this: Element, element: Element) => void),
handler?: (this: Element, element: Element) => void,
) => void;
type LeaveSignature = (
element: string,
handlerOrOptions: ((this: Element, element: Element) => void) | Options,
handler?: (this: Element) => void,
) => void;
type UnbindLeaveSignature = (

  1. The 3rd-parameter handler in LeaveSignature is typed as (this: Element) => void, missing the element argument. Callbacks are invoked as cb.callback.call(cb.elem, cb.elem) for both arrive and leave (the element is passed as both this and as the first argument), and the README documents leave callbacks as function(removedElem) { ... }. This should match ArriveSignature's (this: Element, element: Element) => void.

arrive/index.d.ts

Lines 19 to 24 in a420e9e

element: string,
handlerOrOptions: ((this: Element, element: Element) => void) | Options,
handler?: (this: Element) => void,
) => void;
type UnbindLeaveSignature = (
elementOrHandler?: string | ((this: Element, element: Element) => void),

(callback invocation at

arrive/src/arrive.js

Lines 55 to 59 in a420e9e

for (var i = 0, cb; (cb = callbacksToBeCalled[i]); i++) {
if (cb && cb.callback) {
cb.callback.call(cb.elem, cb.elem);
}
}
)

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@oyzamil oyzamil left a comment

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.

Please do review now, Thanks!

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