From a61fbc58da2f8dd676cd71eaba83f424943e257c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Fri, 22 Sep 2023 19:45:04 +0200 Subject: [PATCH] fix: Correct type definitions for createElement function --- package.json | 2 +- src/createElement.ts | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 60b5238..a1a1ae5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/createElement.ts b/src/createElement.ts index 1a79107..d0ba508 100644 --- a/src/createElement.ts +++ b/src/createElement.ts @@ -22,7 +22,8 @@ */ export default function createElement( tagName: K, - options: CreateElementOptions = {}, + options: Partial> & + SpecialAttributes = {}, target = document, ): HTMLElementTagNameMap[K] { const element = target.createElement(tagName); @@ -50,16 +51,19 @@ export default function createElement( 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; - text?: string; -}; +export type SpecialAttributes = Partial<{ + class: string | string[]; + dataset: Record; + text: string; +}>;