在 TypeScript 中输入 gql-tag

2024-02-01

我正在使用 GraphQL,并且希望严格输入gql。是否有可能使result变量的形状ResultData即使用最新版本的 TypeScript。它只与输入有关,与运行时无关。

interface Data {
    one: string;
    two: number;
    three: {
        four: string;
        five: {
            six: Date;
        };
    };
}

// true means that this field must be added to query
type RecursivePartial<T> = {[P in keyof T]?: RecursivePartial<T[P]> | true};

function gql<T>(fields: RecursivePartial<T>) {
  // Some code about generating GraphQL query
}

const result = gql<Data>({one: true, three: {five: {six: true}}});

// type ResultData {
//     one: string;
//     three: {
//         five: {
//             six: Date;
//         };
//     };
// }

这在操场上对我有用:

interface Data {
    one: string;
    two: number;
    three: {
        four: string;
        five: {
            six: Date;
        };
    };
}

// Prevent widening of true to boolean in queries
type RecursivePartial1 = { [k: string]: RecursivePartial1 | true };
// true means that this field must be added to query
type RecursivePartial<T> = RecursivePartial1 & { [P in keyof T]?: RecursivePartial<T[P]> | boolean };

type RecursivePick<T, Q extends RecursivePartial<T>> =
  { [P in keyof T & keyof Q]:
    Q[P] extends RecursivePartial<T[P]> ? RecursivePick<T[P], Q[P]> : T[P] };

function gql<T, Q extends RecursivePartial<T>>(data: T, fields: Q): RecursivePick<T, Q> {
  // Some code about generating GraphQL query
}

declare const data: Data;
const result = gql(data, { one: true, three: { five: { six: true } } });

// type ResultData {
//     one: string;
//     three: {
//         five: {
//             six: Date;
//         };
//     };
// }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 TypeScript 中输入 gql-tag 的相关文章

随机推荐

  • 如何使用 C++ 中的构造函数初始化 2d 向量?

    我知道如何像这样初始化一维向量 int myints 16 2 77 29 std vector
  • Mysql,SUM 列和 GROUP BY HOUR

    情况 简化 tableA id date val 0 2018 02 19 00 01 00 10 1 2018 02 19 00 02 00 10 2 2018 02 19 00 03 00 10 2018 02 19 23 59 00
  • 如何使用 Cef4Delphi 从 JavaScript 调用 Delphi 函数

    我是德尔福的初学者 目前使用Delphi Berlin 版本 我正在尝试从 JavaScript 调用 Delphi 函数 方法 例如 我想在单击带有附加数据属性的 html 按钮时打开一个新的 Delphi 表单 HTML代码
  • 如何禁用选项卡栏中的特定选项卡才能单击?

    有没有办法禁用选项卡栏中的特定选项卡 这样除非再次启用 否则无法单击它 感谢任何帮助 谢谢 编辑 吸收 忽略指针的代码不起作用 class MyTabbedPage extends StatefulWidget const MyTabbed
  • 如何将具有像 ResNet 这样的非序列架构的 Keras 模型拆分为子模型?

    我的模型是 resnet 152 我想将其切成两个子模型 问题是第二个子模型 我不知道如何构建从中间层到输出的模型 我尝试了这段代码这个回应 https stackoverflow com questions 52800025 keras
  • 元组列表的列表,按第一个元素分组并添加第二个元素

    假设我有以下元组列表 tuples 2017 04 11 2000000 00 2017 04 12 1000000 00 2017 04 13 3000000 00 2017 04 12 472943 00 2017 04 13 1000
  • JavaScript 浮点好奇心

    我尝试进行一些浮点比较 这是我发现的 130 130 000000000000014210854715 true 130 130 000000000000014210854716 false 9 9 00000000000000088817
  • 我可以在猫鼬聚合之前使用填充吗?

    我有两种模型 一种是用户 userSchema new Schema userID String age Number 另一个是所有用户每天多次记录的分数 ScoreSchema new Schema userID type String
  • C++中继承私有成员

    假设一个类具有私有数据成员 但 setter 和 getter 位于公共范围内 如果从此类继承 您仍然可以调用这些 setter 和 getter 从而能够访问基类中的私有数据成员 既然提到派生类不能继承私有数据成员 这怎么可能 派生类不继
  • 将多个变量传递给内容脚本 chrome

    我正在编写我的第一个 chrome 扩展 几个小时前才开始 当硬编码时一切都运行良好 本质上 我正在填写一份 8 页的表格 表单的每个页面对应一个单独的内容脚本 内容脚本过去看起来像这样 查找字段有点困难 因为它们不是标准的 但我并不担心
  • 按照惯例,Java .class 文件存储在哪里?

    我有一个 Java src 文件夹 在其中存储 java 文件 然后 我使用终端编译它们 并最终在同一目录中获取 class 文件 这不一定会困扰我 但我从未见过专业人士这样做过 按照专业惯例 如果存在 应将编译后的 class 文件存储在
  • 如何在flutter中实现自定义对话框?

    我是 flutter 新手 需要创建一个图库应用程序 该应用程序需要自定义对话框来显示所选图像 我怎样才能实现呢 使用 Flutter 中 AlertDialog 类的父类 Dialog 类 对话框小部件有一个参数 shape 您可以使用它
  • MATLAB - 具有布尔值的棘手颂歌系统

    编辑 感谢您的支持 现在我终于添加了图像 添加了完整的 m file 尽管我认为没有必要 代码的关键是 xp 2 x 2 gt X2 xp 3 gt 0 xp 3 x 3 gt X3 xp 2 gt 0 完整代码 function xp u
  • Guice的injectMembers方法

    我了解使用构造函数注入相对于 setter 注入的好处 但在某些情况下我必须坚持仅使用基于 setter 的注入 我的问题是如何使用注入所有基于设置器的注入类的成员injector injectMembers method I am cal
  • getGenericParameterTypes 和 getParameterTypes 之间的区别

    我正在尝试了解之间的区别getGenericParameterTypes and getParameterTypes方法 我知道有人回来了Class 和另一个Type 但真正的区别是什么 考虑方法 public void method1 T
  • Git克隆存储库错误:RPC失败;结果=56,HTTP 代码=200

    我已经使用 Git 存储库几年了 但仍然感觉像个新手 非常欢迎帮助 它开始克隆一段时间 remote Counting objects 22394 br remote Compressing objects 100 12314 12314
  • iconv 返回奇怪的结果

    我正在研究一种方法来解决在 PHP 中创建帐户的自动脚本中使用特殊字符的问题 由于电子邮件地址和其他地方不需要特殊字符 因此我试图删除它们 但在将它们提供给脚本之前我无法删除它们 因为用户名必须正确显示给其他用户 例子 J rgen G t
  • scala 类中属性的可见性

    我通过以下方式在类的构造函数中定义了一个属性 class Step val message String 当我尝试访问时message从 Java 代码中获取可见性错误的值 为什么 如果添加 scala reflect BeanProper
  • 块递归和破坏保留周期

    为了更好地说明问题 请考虑以下块递归的简化形式 block void next int int index if index 3 return int i index next i next 0 XCode 启用 ARC 警告 在此块中强烈
  • 在 TypeScript 中输入 gql-tag

    我正在使用 GraphQL 并且希望严格输入gql 是否有可能使result变量的形状ResultData即使用最新版本的 TypeScript 它只与输入有关 与运行时无关 interface Data one string two nu