【发布时间】:2020-03-12 01:55:08
【问题描述】:
我正在尝试在不破坏公共 API 的情况下将此 JavaScript 代码从 atom/etch 转换为 TypeScript。
它同时定义了一个dom-function 和一个dom-array of functions(都具有相同的名称dom):
// called by the following loop
function dom (tag, props, ...children) {
// ...
}
const HTML_TAGS = [
'a', 'abbr'] // ... has more elements though
// similarly SVG-Tags is defined
// finds the array of functions
for (const tagName of HTML_TAGS) {
dom[tagName] = (props, ...children) => {
return dom(tagName, props, ...children)
}
}
for (const tagName of SVG_TAGS) {
dom[tagName] = (props, ...children) => {
return dom(tagName, props, ...children)
}
}
module.exports = dom
什么是等效的 TypeScript 版本?
其他包使用 dom 比如dom.a(tag, props, childern)an example,或者使用@jsx etch.doman example
通过运行dts-gen,我得到一个名为dom 的命名空间,其中包含在for 循环中定义的所有函数。
export namespace dom {
function a(props: any, children: any): any;
function abbr(props: any, children: any): any;
//...
}
这里是my branch。
【问题讨论】:
标签: javascript typescript metaprogramming