简体   繁体   English

如何通过Typescript中的点表示法或括号表示法访问object?

[英]How to access object by dot notation or bracket notation in Typescript?

I'm new to typescript.我是 typescript 的新手。

I created a simple class with a getter.我用吸气剂创建了一个简单的 class。

But it's not accessible to a child value of the getter object.但是 getter object 的子值无法访问它。

What's wrong with my codes?我的代码有什么问题?

class TestObjectWrapper {
  private _obj: Object;

  constructor(_obj: Object) {
    this._obj = { ..._obj };
  }

  get obj(): Object {
    return { ...this._obj };
  }
}

const testObj1 = { a: '01', b: '02' };
const testInstance = new TestObjectWrapper(testObj1);


console.log(testObj1); // {"a": "01", "b":"02"}
console.log(testObj1.a); // "01" 

console.log(typeof(testInstance.obj)); // "object"
console.log(testInstance.obj); // {"a": "01", "b":"02"}

Why are the codes below inaccessible?为什么下面的代码无法访问?

I expected to get "01".我希望得到“01”。

console.log(testInstance.obj.a);
//Property 'a' does not exist on type 'Object'.

console.log(testInstance.obj["a"]);
// Element implicitly has an 'any' type because expression of type '"a"' can't be used to index type 'Object'.
  Property 'a' does not exist on type 'Object'.

The Object type used in TestObjectWrapper only has properties that all objects share, and a isn't one of them. TestObjectWrapper中使用的Object类型仅具有所有对象共享的属性,而a不是其中之一。

You can fix this by inferring a generic type T in TestObjectWrapper :您可以通过在TestObjectWrapper中推断泛型类型T来解决此问题:

class TestObjectWrapper<T> {
  private _obj: T;

  constructor(_obj: T) {
    this._obj = { ..._obj };
  }

  get obj(): T {
    return { ...this._obj };
  }
}

It would be good if you type your private property and getter correctly and then accessing it.如果您正确键入您的私有财产和吸气剂然后访问它,那将是一件好事。 You can change your code like below and this way you can remove this error.您可以像下面这样更改您的代码,这样您就可以消除此错误。

interface MyObj {
  a: string;
  b: string
}

class TestObjectWrapper {
  private _obj: MyObj;

  constructor(_obj: MyObj ) {
    this._obj = { ..._obj };
  }

  get obj(): MyObj {
    return { ...this._obj };
  }
}

const testObj1 = { a: '01', b: '02' };
const testInstance = new TestObjectWrapper(testObj1);
let gettedObj = testInstance.obj;
console.log(gettedObj.a);
console.log(testInstance.obj["a"]);

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

相关问题 如何在 TypeScript 严格模式下使用方括号表示法访问对象的属性 - How to access a property of an object using the bracket notation in TypeScript strict mode 无法在打字稿中使用点符号访问对象属性 - Unable to access object properties with dot notation in typescript Typescript:括号表示法属性访问 - Typescript: bracket notation property access 如何在 JavaScript/Typescript 中轻松地将方括号表示法 (abc[&#39;prop&#39;]) 替换为点表示法 (abc.prop)? - How to replace square bracket notation (abc['prop']) to dot notation (abc.prop) in JavaScript/Typescript easily? Typescript 使用括号表示法访问 object 的可选属性 - Typescript using bracket notation to access optional properties of an object Typescript:使用方括号表示法和变量访问 object 属性 - Typescript: access an object property with square bracket notation and variable 使用 TypeScript 中括号符号中的变量访问 object 属性 - Access object property using variable in bracket notation in TypeScript 可以使用方括号表示法访问模块属性,但不能使用点属性表示法 - Can access module property with square bracket notation but not with dot property notation typescript 中括号符号的类型 - Types for bracket notation in typescript 在 angular 中将点表示法转换为括号表示法 - Convert dot notation to bracket notation in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM