简体   繁体   English

使用Object.defineProperty动态创建的类型检查属性

[英]Type-checking properties created dynamically with Object.defineProperty

I have this handy construct like so: 我有这样方便的构造:

export class LinkedQueue {

  private lookup = new Map<any, any>();
  private head = null as any;
  private tail = null as any;
  public length: number;

  constructor() {

    Object.defineProperty(this, 'length', {
      get: () => {
        return this.lookup.size;
      }
    });

  }

} 

note that if I remove this line: 请注意,如果我删除此行:

 public length: number;

it still compiles, even though it probably shouldn't. 它仍然可以编译,即使它可能不应该编译。 So my question is - is there a way to type check dynamically created properties like that? 所以我的问题是-有没有办法键入检查像这样动态创建的属性? I would assume if it's a hardcoded string like 'length', then it would be possible. 我会假设它是一个像'length'这样的硬编码字符串,那么这是有可能的。

Here are my tsconfig.json settings: 这是我的tsconfig.json设置:

{
  "compilerOptions": {
    "outDir":"dist",
    "allowJs": false,
    "pretty": true,
    "skipLibCheck": true,
    "declaration": true,
    "baseUrl": ".",
    "target": "es6",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "allowUnreachableCode": true,
    "lib": [
      "es2015",
      "es2016",
      "es2017"
    ]
  },
  "compileOnSave": false,
  "exclude": [
    "test",
    "node_modules"
  ],
  "include": [
    "src"
  ]
}

Object.defineProperty(this, 'length', { is not type checked for how its mutating this . Object.defineProperty(this, 'length', {类型进行类型检查,以检查其如何使this突变。

Alternate 备用

You can actually define a getter that compiles to the same thing 您实际上可以定义一个可编译为同一事物的吸气剂

export class LinkedQueue {

  private lookup = new Map<any, any>();
  private head = null as any;
  private tail = null as any;

  constructor() {
  }

  get length() { 
    return this.lookup.size;
  } 

} 

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

相关问题 确保 spyOnProperty 使用 Object.defineProperty 创建可配置的属性 - Ensure spyOnProperty creates configurable properties with Object.defineProperty 使用Object.defineProperty后如何读取typescript中的属性? - How to read properties in typescript after using Object.defineProperty? 使用 Object.defineProperty 时,类型中缺少属性,但类型中需要属性 - Property is missing in type but required in type when using Object.defineProperty 打字稿:通过`Object.defineProperty`向数组类型添加别名 - Typescript: adding aliases via `Object.defineProperty` to an array type Object.defineProperty(exports,“__ myModule”,{value:true}); - Object.defineProperty(exports, “__esModule”, { value: true }); Object.defineProperty在构造函数中不执行任何操作 - Object.defineProperty does nothing in constructor 如何在 Typescript 中为 getter 使用 Object.defineProperty? - How to use Object.defineProperty for getters in Typescript? TypeScript 键入:Object.defineProperty function 参数 - TypeScript typing: Object.defineProperty function args 在返回的对象中类型检查正确的键值使用 - Type-checking correct key-value usage in a returned object 对对象的角度HttpParams,Object.defineProperty创建属性,但超出范围 - Angular HttpParams to object, Object.defineProperty creates property but is out of scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM