Typescript with TypeLite - Run time type checking

喜你入骨 提交于 2019-12-06 05:45:28

问题


Let's say I have some C# DTO's and I want to convert them to TypeScript interfaces using T4 templates and neat little library called TypeLite

On the client side, I have some concrete TypeScript classes (that inherit from Backbone.Model but that's not important) that represent the same DTO defined on the server side.

The intended goal of the interfaces is to act as data contracts and ensure client and server DTOs are kept in sync.

However, this is problematic since TypeScript supports no run-time type checking facilities other than instanceof. The problem with instance of is when I fetch my DTOs from the server they are plain JSON objects and not instances of my model. I need to perform run-time type checking on these DTOs that come in from the server as JSON objects.

I know I can do something like this:

collection.fetch({...}).done((baseModels) => {
     baseModels.forEach((baseModel) => {
           if(baseModel&& baseModel.SomeProperty && baseModel.SomeOtherProperty){
                //JSON model has been "type-checked"
           }
     });
});

However, there is obvious problems to this because now I need to update in three places if I change or add a property.

Currently the only thing I found is this but it's undocumented, not maintained, and uses node which I have zero experience with so I'll save myself the frustration. Does anybody know of anything similar to perform run-time type checking in TypeScript or some other way to accomplish what I'm after?

It would be great if this was built into TypeLite to generate the interfaces as well as a JSON schema for type checking at run-time. Being that this project is open source somebody should really go ahead and extend it. I'd need some pointers at the least if I would do it myself (thus the question).

More details about my particular problem here (not necessary but if needed extra context)


回答1:


At runtime you are using plain JavaScript, so you could use this answer as it relates to plain JavaScript:

How do I get the name of an object's type in JavaScript?

Here is a TypeScript get-class-name implementation that can supply the name of the enclosing TypeScript class (the link also has a static separate version of this example).

class ShoutMyName {
    getName() { 
        var funcNameRegex = /function (.{1,})\(/;
        var anyThis = <any> this;
        var results = (funcNameRegex).exec(anyThis.constructor.toString());
        return (results && results.length > 1) ? results[1] : "";
    }
}

class Example extends ShoutMyName {
}

class AnotherClass extends ShoutMyName {
}

var x = new Example();
var y = new AnotherClass();

alert(x.getName());
alert(y.getName());

This doesn't give you data about the inheritance chain, just the class you are inspecting.



来源:https://stackoverflow.com/questions/15775092/typescript-with-typelite-run-time-type-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!