简体   繁体   English

打字稿:防止使用“.name”来获取函数/类名

[英]Typescript: prevent use of ".name" to get function/class name

is there an easy way (maybe tslint rule) to prevent us from using MyClass.name, or myFunction.name?有没有一种简单的方法(可能是 tslint 规则)来阻止我们使用 MyClass.name 或 myFunction.name?

We're trying to make sure no Dev end up using this, as the minification process will change method names.我们正在努力确保没有开发人员最终使用它,因为缩小过程将更改方法名称。

Thank you!谢谢!

Untested, so mileage may vary from 0 to 100, but you can try the following.未经测试,因此里程数可能从 0 到 100 不等,但您可以尝试以下操作。 Inside a global/ambient ts file write:在全局/环境 ts 文件中写入:

declare global {
  interface Function {
    /** @deprecated Don't use this, think about the children!*/
    readonly name: string;
  }
}

Then set in your tslint config:然后在你的 tslint 配置中设置:

"rules": {
  "deprecation": true
}

Perhaps this will warn the developer with a deprecation notice, but there is also a possibility it's ignored, because the lib typings do not have this comment也许这会用弃用通知警告开发人员,但也有可能被忽略,因为 lib 类型没有此注释

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

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