带TypeLite的Typescript-运行时类型检查 [英] Typescript with TypeLite - Run time type checking

查看:207
本文介绍了带TypeLite的Typescript-运行时类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些C#DTO,我想使用T4模板和名为 TypeLite

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

在客户端,我有一些具体的TypeScript类(继承自Backbone.Model,但这并不重要),它们代表在服务器端定义的同一DTO.

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.

接口的预期目标是充当数据合同,并确保客户端和服务器DTO保持同步.

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

但是,这是有问题的,因为TypeScript除了instanceof之外不支持任何运行时类型检查工具.实例的问题是,当我从服务器获取DTO时,它们是普通的JSON对象,而不是模型的实例.我需要对来自服务器作为JSON对象的这些DTO执行运行时类型检查.

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.

目前,我发现的唯一内容是,但未记录,没有维护,并使用我零经验的节点,所以我可以避免烦恼.有人知道有什么类似的事情可以在TypeScript中执行运行时类型检查或通过其他方式完成我要执行的工作吗?

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?

如果将它内置到TypeLite中以生成接口以及JSON模式以在运行时进行类型检查,那将是很好的.既然这个项目是开源,那么有人真的应该继续进行并扩展它.如果我自己做,则至少需要一些指针(因此是问题).

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)

推荐答案

在运行时,您使用的是纯JavaScript,因此您可以使用以下答案,因为它与纯JavaScript有关:

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

如何获取JavaScript中对象类型的名称?

这是 TypeScript get-class-name实现,它可以提供封闭的TypeScript类的名称(该链接还具有此示例的静态单独版本).

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.

这篇关于带TypeLite的Typescript-运行时类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆