Skip to content

Add typing jq.js#12

Open
rfon6ngy wants to merge 2 commits into
mwh:masterfrom
rfon6ngy:patch-1
Open

Add typing jq.js#12
rfon6ngy wants to merge 2 commits into
mwh:masterfrom
rfon6ngy:patch-1

Conversation

@rfon6ngy

@rfon6ngy rfon6ngy commented May 24, 2026

Copy link
Copy Markdown
Contributor

Having a strongly typed jq.js would prevent regression and help contribution (safer review)

The current jq.js has 707 problems (most are because of weak typing, but some are more serious),
most of the weak typing issues can be fixed by :

  • annotating function param with the correct @types
  • replacing nameType(x) with typeof or instanceof to unlock type inference on the used variable (keep nameType for message display of the type)

Doing this drop the number of problems to 327

some bugs where discovered and fixed along the way:

  • duplicate fromjson/0 definition
  • AlternativeOperator 's .toString() use incorrect properties: this.l + this.r
  • invalid sh .toString() value that print the type instead of the value
  • arithmetic operation on a Date() Object without explicit .valueOf()

to go little further (if okay for you), we could:

  • cleanly defining the functions list to an Array<{func,params}>
  • explicit handle of null (eg. tonumber/0 does not check type and can do a Number.parseFloat(null)) (is JQValue possibly null ?)
  • use of trimRight instead of deprecated trimEnd

Doing this drop the number of problems to 193, we could add the missing typing annotation to drop bellow 100, but the remaining 100 issues would require a more skilled overview than me (or be ready for me asking lot of confirmation).

By the way, this lib keep impressing me. It's a hidden gem in the bland ocean of javascript ecosystem.

rfon6ngy added 2 commits May 24, 2026 01:59
Only 327 remaining typing issues

2 or 3 bug where fixed along the way
@mwh

mwh commented May 24, 2026

Copy link
Copy Markdown
Owner

Thanks, this looks useful. You can see I started but only put external-facing types in. I'll have to look through this closely and see about figuring out those other complex cases; I do wonder whether some parts of the code are fundamentally untypeable (or typed "any and trust me") because there are pieces of action-at-a-distance type juggling where it's trying to align with base jq semantics. I don't want to add redundant instanceof cases just for a typechecker where everything does line up by construction.

The second commit seems to be almost entirely noise with some deep functional changes buried within it.

I think it'd be better to have prefix jsdoc blocks rather than inline type comments strewn through the parameters. Then they'd have space for documentation too.

I'm probably not going to get onto this in depth until sometime next week, but it'd be nice to get everything to an internally type-safe state if it's not too intrusive.

@rfon6ngy

Copy link
Copy Markdown
Contributor Author

yeah sorry for the second commit, I ctrl+shift+f (format) the code while working on the functions/mkfs

I'll rework/reorder those commit for easier review and i'll ping you

I tried to avoid touching the code but sometimes it's required (see:mkfn and use of typeof) to help the inference

I agree that it's sometimes difficult to type some params, but explicit typing as any is acceptable

There is absolutely no rush for this PR because I'll try to

  • finish fixing all issue (even if it's dirty)
  • make a quick summary of what I tried and confirm with you the strategy
  • redo it better according to your feedbacks/pointers

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