简体   繁体   English

如何获取类似于 Function.name 的属性名称?

[英]How to get an attribute name similar to Function.name?

My goal is to safely type an attribute, eg I have an object我的目标是安全地键入一个属性,例如我有一个 object

const identity = {
  address: "Jackie"
};

At some point, I will change the address key to something else eg street_address .在某些时候,我会将address键更改为其他内容,例如street_address

This is how I currently use it:这是我目前使用它的方式:

<input name="address" value={identity.address} />

Sometimes I forget to update the name attribute to street_address as well.有时我也忘记将name属性更新为street_address

I wish to be able to use something similar to the Function objejct Function.name我希望能够使用类似于 Function 对象Function.name的东西

<input name={identity.address.name} value={identity.address} />

The answer is to create a helper function.答案是创建一个助手 function。

Here's the SO's answer:这是SO的答案:

nameof keyword in Typescript Typescript 中的 nameof 关键字

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM