Skip to content
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

doc: add dynamic return types #16

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "ajquery.cjs",
"module": "ajquery.mjs",
"type": "commonjs",
"types": "./select-dom.d.ts",
"browser": {
"ajquery.min.cjs": "ajquery.min.js"
},
Expand All @@ -21,7 +22,8 @@
"ajquery.cjs",
"ajquery.min.cjs",
"ajquery.mjs",
"ajquery.min.mjs"
"ajquery.min.mjs",
"select-dom.d.ts"
],
"scripts": {
"benchmark": "node ./benchmark.js",
Expand Down Expand Up @@ -62,5 +64,8 @@
"bugs": {
"url": "https://github.com/coolaj86/ajquery.js/issues"
},
"homepage": "https://twitter.com/coolaj86/status/1303386788119998464"
"homepage": "https://twitter.com/coolaj86/status/1303386788119998464",
"devDependencies": {
"typed-query-selector": "^2.12.0"
}
}
89 changes: 89 additions & 0 deletions select-dom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* select-dom (https://github.com/fregante/select-dom)
* MIT License
* Copyright (c) Federico Brigante Federico Brigante <[email protected]> (https://fregante.com)
* Copyright (c) 2014 Azer Koçulu <[email protected]>
*/

import type { ParseSelector } from "typed-query-selector/parser.js";

type BaseElements = ParentNode | Iterable<ParentNode>;

/**
* @param selectors One or more CSS selectors separated by commas
* @param [baseElement] The element to look inside of
* @return The element found, if any
*/
declare function $<
Selector extends string,
Selected extends Element = ParseSelector<Selector, HTMLElement>,
>(
selectors: Selector | Selector[],
baseElement?: ParentNode
): Selected | undefined;

declare function $<Selected extends Element = HTMLElement>(
selectors: string | string[],
baseElement?: ParentNode
): Selected | undefined;

export declare class ElementNotFoundError extends Error {
name: string;
}

/**
* @param selectors One or more CSS selectors separated by commas
* @param [baseElement] The element to look inside of
* @return The element found, or an error
*/
declare function expectElement<
Selector extends string,
Selected extends Element = ParseSelector<Selector, HTMLElement>,
>(selectors: Selector | Selector[], baseElement?: ParentNode): Selected;
declare function expectElement<Selected extends Element = HTMLElement>(
selectors: string | string[],
baseElement?: ParentNode
): Selected;

/**
* @param selectors One or more CSS selectors separated by commas
* @param [baseElement] The element to look inside of
* @return The element found, if any
*/
declare function lastElement<
Selector extends string,
Selected extends Element = ParseSelector<Selector, HTMLElement>,
>(
selectors: Selector | Selector[],
baseElement?: ParentNode
): Selected | undefined;
declare function lastElement<Selected extends Element = HTMLElement>(
selectors: string | string[],
baseElement?: ParentNode
): Selected | undefined;

/**
* @param selectors One or more CSS selectors separated by commas
* @param [baseElement] The element to look inside of
* @return Whether it's been found
*/
declare function elementExists(
selectors: string | string[],
baseElement?: ParentNode
): boolean;

/**
* @param selectors One or more CSS selectors separated by commas
* @param [baseElements] The element or list of elements to look inside of
* @return An array of elements found
*/
declare function $$<
Selector extends string,
Selected extends Element = ParseSelector<Selector, HTMLElement>,
>(selectors: Selector | Selector[], baseElements?: BaseElements): Selected[];
declare function $$<Selected extends Element = HTMLElement>(
selectors: string | string[],
baseElements?: BaseElements
): Selected[];

export { $, $$, lastElement, elementExists, expectElement };
Loading