类型查询:jQuery风格的类型操作 2021-11-10 默认分类 暂无评论 2077 次阅读  将元组转换为对象 在这篇文章中,我们介绍了一种新的方法来创建TypeScript中使用的类型,它允许我们使用JavaScript内联来轻松生成复杂的类型。 我们使用的语言是TypeType(The Art of Type Programming)。 ## 简介 在`TypeType v0.0.30`中,我们有一个新的类型。`ContextType`,例如:  类型中的总和 这里我们使用语法"```...```"来插入JavaScript代码,并以这种方式实现`globalContext.sum`。  类型背景 "sum "接收`TypeType`中的值和返回`AST`,结果是。  累计金额 这个例子[点击这里](http://www.guobacai.com/index.php/go/url-211-1/ "点击这里") ## 类型查询 一旦我们可以执行外部的`JavaScript`代码,我们就可以实现`libs`来制造一些类似于魔法的东西。 我们知道,将联合类型转换为元组类型是很难的,然而,在`ContextType`中这是微不足道的。  联合到类型中的元组 这里我们使用`"$.use "`来访问类型变量 "input "的AST,并在`JavaScript`中把它转换成`TupleType`。  类型查询扩展 我们使用 "query.install "来添加这个扩展到`$:TypeQuery`对象,灵感来自`jQuery`。 输出结果是:  并联到TS中的元组 顺序保持原样,实际上,你可以按照你的要求重新排序。 `TypeQuery`的扩展性很好,你可以做这样的事情:  并得到结果:  上面的例子可以在以下内容中找到。 [typetype-template/blob/main/src/type-query-extension.ts](http://www.guobacai.com/index.php/go/url-211-2/ "typetype-template/blob/main/src/type-query-extension.ts") [mistlog/typetype-template/blob/main/src/type-query-basic.ts](http://www.guobacai.com/index.php/go/url-211-3/ "mistlog/typetype-template/blob/main/src/type-query-basic.ts") ## 类型测试 我们可以从`Type Runtime`获得`AST`和`type-in-string`,因此我们可以用普通的`jest`来测试类型。  参考资料:[mistlog/typetype-template/blob/main/test/type-runtime.test.ts](http://www.guobacai.com/index.php/go/url-211-4/ "mistlog/typetype-template/blob/main/test/type-runtime.test.ts") ## 实施 代码可以在这里找到:[mistlog/type-query](http://www.guobacai.com/index.php/go/url-211-5/ "mistlog/type-query") 对于快速启动:[mistlog/typetype-template]( http://www.guobacai.com/index.php/go/url-211-6/ "mistlog/typetype-template") 标签: javascript, TypeScript, 类型, 代码, 元组, blob, ast
评论已关闭