-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add typed-query-selector/strict for compile-time selector validation #224
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
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/add-typed-query-selector-strict-again
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+28
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9d4822
Initial plan
Copilot 48e6e9a
Add typed-query-selector/strict for selector validation
Copilot 085a422
Fix linter warning for typed querySelector usage
Copilot 72790dc
Simplify $ helper implementation and improve comments
Copilot edae8bd
Use strict selector types for $ and exists helpers
Copilot 5412d1b
Implement strict selector validation without modifying global types
Copilot 4bc0567
Add strict validation to exists() and demonstrate with invalid selector
Copilot 00b5c9b
Move broken selector from exists() to $() for clearer validation
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Now also intentionally break one of the selectors at random so that I can see typescript typechecking fail. I will manually revert this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 4bc0567. Broke the
isLoggedInselector from'body.logged-in'to'body.logged-in >'(invalid trailing combinator). The inferred return type forexists('body.logged-in >')is nowneverinstead ofboolean, demonstrating that strict validation is working. Note: The build still passes because TypeScript allowsnever(bottom type) to be assigned toboolean, but the inferred type shows the validation caught the invalid selector.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot can you break it in a $() selector? In that case it should become visible and TS should fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 00b5c9b. Moved the broken selector from
exists()to$()ingetLoggedInUser. TypeScript now fails with clear error:The invalid selector
'meta[name="user-login"] >'causes$()to returnnever | undefined, and calling.getAttribute()onnevertriggers the TypeScript error, demonstrating that strict validation is working.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to write the exists() function so that this
neverdoes cause compile time errors? @copilotThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The challenge with
exists()is that it returns a primitiveboolean(orneverfor invalid selectors). Sinceneveris TypeScript's bottom type, it's assignable toboolean, so no compile error occurs.For
$(), errors manifest when calling methods on the result becauseneverhas no properties. Forexists()returning a primitive, there's no such trigger.Potential approaches:
neverinstead ofbooleanfor invalid selectors - IDE shows the type but doesn't fail compilationSelector extends StrictlyParseSelector<Selector> extends never ? string : Selectoras parameter type - but this creates circular type issues{__invalid: never} & booleanfor invalid selectors - but this is overly complexThe current implementation is the most practical - it provides type-level indication in IDEs while keeping the API simple. For compile-time failures that block builds,
$()is more effective since its return value is typically used with method calls.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using === true instead of Boolean()
@copilot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
=== truein the implementation would cause TypeScript error for all cases:This would break valid selectors too since
querySelectorreturnsElement | null, nevertrue.Did you mean something different? For example:
=== trueat call sites?nevermore visible?!== nullinstead ofBoolean()(though functionally equivalent)?The fundamental issue remains:
exists()returns a primitive, so there's no operation on the return value that would fail only forneverbut succeed forboolean.