Skip to content

chore: Align isObject/isRecord usage #1132

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

jsantell
Copy link
Collaborator

@jsantell jsantell commented May 1, 2025

  • Align the multiple, slightly different implementations of isObject (and isRecord) -- sometimes as "non-array" objects, sometimes including arrays, and the differences between object and Record usage.
  • Remove the root entry for @commontools/utils (originally tracking down a bug for something pulling in too many modules, but was misleading). Deno requires a root export, so a compile time (no exports) or runtime error fires if doing so to avoid inadvertantly pulling in all utils

Summary by mrge

Standardized all isObject and isRecord checks across the codebase by providing consistent utility functions in utils/types, and removed duplicate or inconsistent implementations.

  • Refactors
    • Replaced custom isObject/isRecord functions with shared versions from utils/types.
    • Removed unused or duplicate type-checking helpers.
    • Updated imports to use the new shared utilities.

@jsantell jsantell force-pushed the is-object-align branch from 9c33154 to 92ac9e5 Compare May 1, 2025 19:47
@jsantell jsantell requested review from Gozala and seefeldb May 1, 2025 19:50
@jsantell jsantell marked this pull request as ready for review May 1, 2025 19:54
Copy link

@mrge-io mrge-io bot left a comment

Choose a reason for hiding this comment

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

mrge found 1 issue across 20 files. View it in mrge.io

@jsantell jsantell force-pushed the is-object-align branch from 92ac9e5 to fa0396d Compare May 1, 2025 21:59
@jsantell jsantell force-pushed the is-object-align branch from fa0396d to 873815e Compare May 1, 2025 22:00
}
}
};
"intent" in source && typeof source.intent === "string" &&
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: isn't "..." in source redundant here, since isObject(source) already makes sure this is an object and typeof source.<..> would be "undefined" if the key doesn't exist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's what I thought -- the Record type (where Record<number, unknown> could be an array) allows this but object types do not (and had to add it in a few places) -- maybe there's a better way to do this? but we do sometimes explicitly want an object-like ({}) things and not an array or a record

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We'd get a Property 'intent' does not exist on type 'object' otherwise

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, works for me:

image.png

the main problem is that for x = null it'll throw. it's really weird, because it even works for x being a string or a number. but I think you'd be fine if isObject() is true.

(also, doesn't matter for this PR, this got me into TIL mode)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We'd get a Property 'intent' does not exist on type 'object' otherwise

This is a typescript check, and we check that x is not null (isObject(..)) and TS null is not object (unlike JS) -- usually we want those checks ("property does not exist"), though using isRecord previously allowed any key (TIL as well)

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