Typescript - 编译后获取未初始化的属性

Pat*_*eck 1 reflection class socket.io typescript

我目前正在编写一个围绕 socket.io 的包装器。来自非常面向对象的背景,我想在我的框架/包装器中实现模型的概念。

如果您碰巧了解 socket.io,您可能知道您获取与事件关联的数据作为参数,现在我已经实现了一个自定义路由系统,其中路由的处理程序获取类似express.js 的请求中的数据目的。

这个想法是让模型类看起来像这样:

class XRequestModel
  @v.String({ message: 'The username must be a string!' })
  public userName: string;
}
Run Code Online (Sandbox Code Playgroud)

路线事件可能如下所示:

@RouteConfig({ route: '/something', model: XRequestModel })
class XEvent extends Route {
  public on(req: Request<XRequestModel>, res: Response) {
    // Handle Event
  }
}
Run Code Online (Sandbox Code Playgroud)

为了完成这里的示例,请求对象可能如下所示:

class Request<T> {
  public data: T;
}
Run Code Online (Sandbox Code Playgroud)

现在打字稿中的泛型非常有限,因为类型信息在编译后被删除,我无法使用泛型请求参数(这是模型的类型)从模型中获取元数据 - 在这种情况下,元数据是验证装饰器。为了解决这个问题,我将 Model 类引用到 RouteEvent 的 RouteConfig,该类在内部使用,允许我创建模型的实例、获取属性等等...

这里的想法是为路由处理程序提供一个带有预先验证的类型安全数据的请求对象。

阻碍我这样做的事情是,未使用的属性在通过打字稿编译后被删除,所以我无法获取模型的元数据。初始化类属性可以解决这个问题:

class XRequestModel
  @v.String({ message: 'The username must be a string!' })
  public userName: string = '';
}
Run Code Online (Sandbox Code Playgroud)

但我认为这会导致一些非常冗长的语法,而且我不想强迫这个包装器的用户初始化所有模型属性。

实施旁注:

框架的用户必须将类注册到“主”类,然后我可以通过装饰器反射获取路由类。

当我尝试在没有初始化属性的情况下获取模型的属性时 - 第一个模型示例。

// Here the route.config.model refers to the model from the RouteConfig
Object.getOwnPropertyNames(new route.config.model());
>>> []
Run Code Online (Sandbox Code Playgroud)

这是我通过初始化属性得到的结果:

Object.getOwnPropertyNames(new route.config.model());
>>> [ 'userName' ]
Run Code Online (Sandbox Code Playgroud)

这里是 GitHub 存储库的链接:https://github.com/FetzenRndy/SRocket 请注意,此存储库中尚未实现模型。

基本上,我的问题是:如何获取编译后具有未初始化属性的类的属性。

Tit*_*mir 7

问题是,如果没有进行初始化,则不会为字段发出任何代码,因此在运行时,在为该字段分配值之前,该字段并不存在于对象上。

最简单的解决方案是初始化所有字段,即使您只使用 null :

class XRequestModel {
    public userName: string = null;
    public name: string = null;
}
var keys = Object.getOwnPropertyNames(new XRequestModel())
console.log(keys); // [ 'userName', 'name' ]
Run Code Online (Sandbox Code Playgroud)

如果这对您来说不是一个可行的解决方案,您可以创建一个装饰器,添加到类上的静态字段,然后沿着原型链向上获取所有字段:

function Prop(): PropertyDecorator {
    return (target: Object, propertyKey: string): void => {
        let props: string[]
        if (target.hasOwnProperty("__props__")) {
            props = (target as any)["__props__"];
        } else {
            props = (target as any)["__props__"] = [];
        }
        props.push(propertyKey);
    };
}

class XRequestModelBase {
    @Prop()
    public baseName: string;
}

class XRequestModel extends XRequestModelBase {
    @Prop()
    public userName: string;
    @Prop()
    public name: string;
}
function getAllProps(cls: new (...args: any[]) => any) : string[] {
    let result: string[] = [];
    let prototype = cls.prototype;
    while(prototype != null) {
        let props: string[] = prototype["__props__"];
        if(props){
            result.push(...props);
        }
        prototype = Object.getPrototypeOf(prototype);
    }
    return  result;
}
var keys = getAllProps(XRequestModel);
console.log(keys); 
Run Code Online (Sandbox Code Playgroud)