简体   繁体   English

我如何在 Typescript 中对这个 object 进行类型检查

[英]How do I type-check this object in Typescript

I am in a state of confusion on how I would go about type-checking this variable, but I am not able to do it.我对如何对这个变量进行类型检查感到困惑,但我无法做到。

What I understand is that this is an object which contains fields, but these fields are written in a obscure way.我的理解是,这是一个包含字段的 object,但这些字段是以一种晦涩的方式编写的。

The object is called userApproval object 被称为 userApproval

Here is the object when I output it to console.这是 object 当我 output 它到控制台时。

{
l8s.restart_trigger: true, l8s.system_scale: true, manager.cleanup: true, 
manager.extend: true
}

Now this is the type I am attempting to type to,现在这是我尝试输入的类型,

 export type UserApproval = {
  MANAGER_CLEANUP: boolean;
  MANAGER_EXTEND: boolean;
  L8S_SYSTEM_SCALE: boolean;
  L8S_RESTART_TRIGGER: boolean;
};

Is this correct这个对吗

I think the keys of your object are example.test so based on that try this types.我认为您的 object 的键是example.test因此基于此尝试此类型。

type ApprovalTypes =  {
  'l8s.restart_trigger': boolean;
  'l8s.system_scale': boolean;
  'manager.cleanup': boolean;
  'manager.extend': boolean;
}

const data:ApprovalTypes = {
  'l8s.restart_trigger': true, 
  'l8s.system_scale': true, 
  'manager.cleanup': true, 
  'manager.extend': true
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Flow类型执行写运行时类型检查表达式? - How can I do a write run-time type-check expression using Flow type? 如何使用 TypeScript 编译器 API 对使用 `require()` 导入的模块进行类型检查? - How to use the TypeScript Compiler API to type-check modules imported using `require()`? 我是否应该(静态地)对第三方代码进行类型检查(例如使用Flow)? - Should I (statically) type-check Third-party codes (e.g. using Flow)? 打字稿:如何在对象映射器中缩小类型? - Typescript: how to do type narrowing in an object mapper? 如何检查 object 类型的原型是否为 Typescript 中的 Object.prototype? - How to check if an object type's prototype is Object.prototype in Typescript? 如何解决 typescript 错误在“对象”类型上找不到带有“字符串”类型参数的索引签名 - How do i solve typescript error No index signature with a parameter of type 'string' was found on type 'Object' 记录中动态键的 TS 类型检查错误 - TS type-check error on dynamic key in record 如何检查当前的 javascript/typescript object 是否属于 ObservableArray 类型? - How to check if current javascript/typescript object is of type of ObservableArray? 当我需要更改对象中的属性类型时,如何正确使用 TypeScript - How do I work with TypeScript correctly when I need to change a type of a property in an object 如何在 React 中根据 typescript 接口检查 JSON object 的类型 - How to check type of a JSON object against typescript interface in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM