Skip to content

Commit

Permalink
fix: Correct type definitions for createElement function
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Sep 22, 2023
1 parent f77abf8 commit a61fbc5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-dom-utils",
"version": "2.0.1",
"version": "2.1.0",
"description": "A simple utility library for DOM manipulation. Provides TypeScript typings for enhanced development experience in TS environments.",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
20 changes: 12 additions & 8 deletions src/createElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/
export default function createElement<K extends keyof HTMLElementTagNameMap>(
tagName: K,
options: CreateElementOptions = {},
options: Partial<Omit<HTMLElementTagNameMap[K], keyof SpecialAttributes>> &
SpecialAttributes = {},
target = document,
): HTMLElementTagNameMap[K] {
const element = target.createElement(tagName);
Expand Down Expand Up @@ -50,16 +51,19 @@ export default function createElement<K extends keyof HTMLElementTagNameMap>(
return;
}

if (key in element) {
(element as any)[key] = value;
return;
}

element.setAttribute(key, value as string);
});

return element;
}

// FIXME try to use string type instead of any
export type CreateElementOptions = {
[key: string]: any;
class?: string | string[];
dataset?: Record<string, string>;
text?: string;
};
export type SpecialAttributes = Partial<{
class: string | string[];
dataset: Record<string, string>;
text: string;
}>;

0 comments on commit a61fbc5

Please sign in to comment.