Skip to content

Commit

Permalink
support .closest() (close #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Apr 28, 2021
1 parent 078f191 commit 5e82262
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Support `.closest()` method on `Element`. (Close [#16](https://github.com/g-plane/typed-query-selector/issues/16))

## v2.5.3

- Fixed a regression bug which is introduced in v2.5.2.
Expand Down
4 changes: 4 additions & 0 deletions shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ declare global {
selector: S,
): NodeListOf<ParseSelector<S>>
}

interface Element {
closest<S extends string>(selector: S): ParseSelector<S> | null
}
}
5 changes: 5 additions & 0 deletions shim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ const results: Array<HTMLAnchorElement | null> = []
results.push(document.querySelector('div'))
// @ts-expect-error
results.push(document.querySelector('div#app'))

const closest = el!.closest('button.btn-confirm, a.link')
type TestClosest = Expect<
Equal<typeof closest, HTMLButtonElement | HTMLAnchorElement | null>
>
6 changes: 6 additions & 0 deletions strict.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ declare global {
selector: S,
): [E] extends [never] ? never : NodeListOf<E>
}

interface Element {
closest<S extends string, E extends Parse<S>>(
selector: S,
): [E] extends [never] ? never : E | null
}
}
7 changes: 7 additions & 0 deletions strict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ const results: Array<HTMLAnchorElement | null> = []
results.push(document.querySelector('div'))
// @ts-expect-error
results.push(document.querySelector('div#app'))

const closest = a1!.closest('button.btn-confirm, a.link')
type TestClosest = Expect<
Equal<typeof closest, HTMLButtonElement | HTMLAnchorElement | null>
>
const invalidClosest = a1!.closest('button.btn-confirm, a.')
type TestInvalidClosest = Expect<Equal<typeof invalidClosest, never>>

0 comments on commit 5e82262

Please sign in to comment.