From d7a38d71b7ce8d6f0d4ec2838c5679e3aaa2bcea Mon Sep 17 00:00:00 2001 From: Tobias Lidskog Date: Tue, 13 Mar 2018 09:25:58 +0100 Subject: [PATCH] Refactor to remove function casting. Casting the setAttribute and setAttributeNS functions at each call site make the code harder to read. Instead extract the new overloads for Element into a global declare statement. This does not change the generated javascript code, just makes the typescript easier to read. --- src/modules/attributes.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/modules/attributes.ts b/src/modules/attributes.ts index 875cdaf..47612e8 100755 --- a/src/modules/attributes.ts +++ b/src/modules/attributes.ts @@ -1,6 +1,14 @@ import {VNode, VNodeData} from '../vnode'; import {Module} from './module'; +// because those in TypeScript are too restrictive: https://github.com/Microsoft/TSJS-lib-generator/pull/237 +declare global { + interface Element { + setAttribute(name: string, value: string | number | boolean): void; + setAttributeNS(namespaceURI: string, qualifiedName: string, value: string | number | boolean): void; + } +} + export type Attrs = Record const xlinkNS = 'http://www.w3.org/1999/xlink'; @@ -28,19 +36,16 @@ function updateAttrs(oldVnode: VNode, vnode: VNode): void { } else if (cur === false) { elm.removeAttribute(key); } else { - // because those in TypeScript are too restrictive: https://github.com/Microsoft/TSJS-lib-generator/pull/237 - type SetAttribute = (name: string, value: string | number | boolean) => void; - type SetAttributeNS = (namespaceURI: string, qualifiedName: string, value: string | number | boolean) => void; if (key.charCodeAt(0) !== xChar) { - (elm.setAttribute as SetAttribute)(key, cur); + elm.setAttribute(key, cur); } else if (key.charCodeAt(3) === colonChar) { // Assume xml namespace - (elm.setAttributeNS as SetAttributeNS)(xmlNS, key, cur); + elm.setAttributeNS(xmlNS, key, cur); } else if (key.charCodeAt(5) === colonChar) { // Assume xlink namespace - (elm.setAttributeNS as SetAttributeNS)(xlinkNS, key, cur); + elm.setAttributeNS(xlinkNS, key, cur); } else { - (elm.setAttribute as SetAttribute)(key, cur); + elm.setAttribute(key, cur); } } }