Skip to content

Instantly share code, notes, and snippets.

@mstssk
Created July 22, 2020 05:11
Show Gist options
  • Save mstssk/199c656fb81b7a66489b68955ca1fb5b to your computer and use it in GitHub Desktop.
Save mstssk/199c656fb81b7a66489b68955ca1fb5b to your computer and use it in GitHub Desktop.
/**
* 要素生成のヘルパ
* @param tagName 要素名
* @param props プロパティ
*/
function createElement<
K extends keyof HTMLElementTagNameMap,
AK extends keyof HTMLElementTagNameMap[K]
>(
tagName: K,
props: Record<AK, HTMLElementTagNameMap[K][Extract<AK, string>]>
): HTMLElementTagNameMap[K] {
const elem = document.createElement(tagName);
for (const key in props) {
elem[key] = props[key];
}
return elem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment