首页 > 解决方案 > 获取打字稿函数返回类型作为对象

问题描述

我实际上正在使用方法装饰器进行一些抽象。为此,我实际上想将函数的返回类型作为描述它的对象。不幸的是,我没有找到任何解决方案。

这是一个代码示例

return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    const routeArguments = handleFunctionMetadata(target, propertyKey, descriptor);

    const route = {
        path: path,
        method: 'GET',
        fn: descriptor.value.bind(this),
        arguments: routeArguments
    }

    // I would like to get the return type of descriptor.value here

    if (!target.paths) {
        target.paths = {}
    }

    let key = _.findKey(target.paths, (v) => v === `${route.path}`);

    const docPath = `/${route.path}`;

    const __swaggerRoute = {
        'get': {
            consumes: ["application/json"],
            produces: ["application/json"],
            "summary": "",
            "description": "",
        }
    }

    if (!key) {
        target.paths[docPath] = __swaggerRoute
    } else {
        target.paths[docPath] = _.merge(
            target.paths[docPath],
            __swaggerRoute
        )
    }

    if (!target.routes)
        target.routes = [route];
    else if (_.isArray(target.routes))
        target.routes.push(route)
    return descriptor;
};

标签: node.jstypescriptdecoratorreturn-type

解决方案


推荐阅读