type SomeFunc = <K>(key: keyof ObjType) => ObjType[K]
type Obj = {
field1: string;
field2: number;
field3: boolean;
};
const obj: Obj = {
field1: "some string",
field2: 43,
field3: true,
};
const localKey: keyof Obj = "field2";
const data: Obj[typeof localKey] = obj[localKey]; // number
const getKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>;
type HTMLSwitchElement = HTMLScriptElement | HTMLLinkElement;
type CNIHT = <T extends HTMLSwitchElement>(
node: T,
attr: HTMLAttributes<T>
) => void;
const createNodeInHead: CNIHT = (node, attr) => {
getKeys(attr).forEach((key) => {
node.setAttribute(key, attr[key]);
});
document.head.appendChild(node);
};