Skip to content
This repository was archived by the owner on Oct 21, 2021. It is now read-only.

Commit 98aeb95

Browse files
authored
Merge pull request #3 from xt0rted/typescript-definition
Add a typescript definition file
2 parents 1a304bd + 1402f70 commit 98aeb95

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type Queryable = Document | DocumentFragment | Element;
2+
3+
export function closest<T extends Element = HTMLElement>(context: Queryable, selectors: string, klass?: any): T;
4+
5+
export function query<T extends Element = HTMLElement>(context: Queryable, selectors: string, klass?: any): T;
6+
7+
export function querySelectorAll<T extends Element = HTMLElement>(context: Queryable, selectors: string, klass?: any): Array<T>;
8+
9+
export function namedItem<T extends HTMLFormElement = HTMLFormElement>(form: T, itemName: string, klass?: any): RadioNodeList | Element;
10+
11+
export function getAttribute(element: Element, attributeName: string): string;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
"repository": "github/query-selector",
66
"main": "dist/index.umd.js",
77
"module": "dist/index.esm.js",
8+
"types": "dist/index.d.ts",
89
"scripts": {
910
"clean": "rm -rf dist",
1011
"lint": "eslint index.js test/ && flow check",
1112
"prebuild": "npm run clean && npm run lint && mkdir dist",
1213
"build-umd": "BABEL_ENV=umd babel index.js -o dist/index.umd.js && cp index.js.flow dist/index.umd.js.flow",
1314
"build-esm": "BABEL_ENV=esm babel index.js -o dist/index.esm.js && cp index.js.flow dist/index.esm.js.flow",
14-
"build": "npm run build-umd && npm run build-esm",
15+
"build": "npm run build-umd && npm run build-esm && cp index.d.ts dist/index.d.ts",
1516
"test": "BABEL_ENV=umd mocha --require @babel/register",
1617
"prepublishOnly": "npm run build"
1718
},

test/test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {query, querySelectorAll, closest, getAttribute, namedItem} from "../index";
2+
3+
const bodyElement1 = query(document, 'body');
4+
const bodyElement2 = query(document, 'body', HTMLBodyElement);
5+
const bodyElement3 = query<HTMLBodyElement>(document, 'body');
6+
const bodyElement4 = query<HTMLBodyElement>(document, 'body', HTMLBodyElement);
7+
8+
const formElements1 = querySelectorAll(bodyElement1, '.js-comment-form');
9+
const formElements2 = querySelectorAll(bodyElement2, '.js-comment-form', HTMLFormElement);
10+
const formElements3 = querySelectorAll<HTMLFormElement>(bodyElement3, '.js-comment-form');
11+
const formElements4 = querySelectorAll<HTMLFormElement>(bodyElement4, '.js-comment-form', HTMLFormElement);
12+
13+
const htmlElement1 = closest(bodyElement1, 'html');
14+
const htmlElement2 = closest(bodyElement2, 'html', HTMLHtmlElement);
15+
const htmlElement3 = closest<HTMLHtmlElement>(bodyElement3, 'html');
16+
const htmlElement4 = closest<HTMLHtmlElement>(bodyElement4, 'html', HTMLHtmlElement);
17+
18+
const bodyThing = getAttribute(bodyElement1, 'data-things');
19+
20+
const commentForm = formElements3[0];
21+
22+
const usernameField = namedItem(commentForm, 'username');
23+
const nameField = namedItem(commentForm, 'name', HTMLInputElement);
24+
const emailField = namedItem<HTMLFormElement>(commentForm, 'email', HTMLInputElement);
25+
const mailinglistsField = namedItem<HTMLFormElement>(commentForm, 'mailinglists', RadioNodeList);
26+
const locationField = namedItem<HTMLFormElement>(commentForm, 'location', HTMLSelectElement);

0 commit comments

Comments
 (0)