动态生成类的Typescript接口定义

问题描述 投票:0回答:1

我做了一些相当“不打字”的事情。我有一个函数,将所有以给定基类的“ _”开头的方法混合到另一个动态创建的类中。

而且我正在努力定义正确的返回类型。

这里是一个例子:

export interface IConstructable<T>  {
    new(): T;
}

class Foobar {

    _method() {
        console.log('this method should be copied');
    }

    private method() {
        console.log('this method should not be copied');
    }
}

function createHtml (base: IConstructable<unknown>): IConstructable<HTMLElement> {
    class Html extends HTMLElement {
        constructor(){
            super();
        }
    }

    Object.entries(Object.getOwnPropertyDescriptors(base.prototype)).forEach(([name, descriptor]) => {
        if (name.startsWith('_') && typeof descriptor.value === 'function') {
            Object.defineProperty(Html, name, Object.assign({}, descriptor, {
                value(){
                    // some implementation
                }
            }))
        }
    });

    return Html;
}

const HTMLFoobar = createHtml(Foobar);
new HTMLFoobar()._method();

// I want at least the following interface automatically generated
// 
// interface IHTMLFoobar extends HTMLElement, Foobar {
//    
// }
//
// const HTMLFoobar = createHtml(Foobar) as IConstructable<IHTMLFoobar>;
// new HTMLFoobar()._method();

如果这不可能,那么通过更改*.d.ts文件以编程方式完成此操作,我什至会很好,但是是否有一个项目可以帮助读取和修改/转换*.d.ts文件?

typescript interface type-declaration
1个回答
0
投票

您可以使用通用类型参数代替unknown,并断言所生成的类是HTMLElement和基数的混合:

function createHtml<TBase>(base: IConstructable<TBase>): IConstructable<HTMLElement & TBase> {
    class Html extends HTMLElement {
        constructor(){
            super();
        }
    }

    // ...

    return Html as IConstructable<HTMLElement & TBase>;
}

const HTMLFoobar = createHtml(Foobar);
new HTMLFoobar()._method();

Playground

© www.soinside.com 2019 - 2024. All rights reserved.