From 5e82262cb860cbacda76a0ed9a979f394f14032d Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 28 Apr 2021 08:50:49 +0800 Subject: [PATCH] support `.closest()` (close #16) --- CHANGELOG.md | 2 ++ shim.d.ts | 4 ++++ shim.test.ts | 5 +++++ strict.d.ts | 6 ++++++ strict.test.ts | 7 +++++++ 5 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a3035..65b7dc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/shim.d.ts b/shim.d.ts index 03c3c45..2a6f553 100644 --- a/shim.d.ts +++ b/shim.d.ts @@ -8,4 +8,8 @@ declare global { selector: S, ): NodeListOf> } + + interface Element { + closest(selector: S): ParseSelector | null + } } diff --git a/shim.test.ts b/shim.test.ts index 1a2ca62..442c1c3 100644 --- a/shim.test.ts +++ b/shim.test.ts @@ -19,3 +19,8 @@ const results: Array = [] 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 +> diff --git a/strict.d.ts b/strict.d.ts index ca74ee3..4b32495 100644 --- a/strict.d.ts +++ b/strict.d.ts @@ -79,4 +79,10 @@ declare global { selector: S, ): [E] extends [never] ? never : NodeListOf } + + interface Element { + closest>( + selector: S, + ): [E] extends [never] ? never : E | null + } } diff --git a/strict.test.ts b/strict.test.ts index ce89adc..81cfbad 100644 --- a/strict.test.ts +++ b/strict.test.ts @@ -83,3 +83,10 @@ const results: Array = [] 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 +> +const invalidClosest = a1!.closest('button.btn-confirm, a.') +type TestInvalidClosest = Expect>