我如何防止开发人员使用全局变量?

huangapple go评论60阅读模式
英文:

How can I prevent devs from using a global?

问题

我有一个使用自定义包装器覆盖 chrome.runtime.sendMessage 的TypeScript项目,用于发送消息。这个自定义包装器确保消息格式正确、安全等。

然而,在代码库中经常有开发人员更替,新的开发人员经常尝试直接使用 chrome.runtime.sendMessage(而不是包装器)。

是否有任何方法可以在他们尝试访问该方法时,在编译时或在他们的IDE中显示警告?

英文:

I have a TypeScript project that uses a custom wrapper over chrome.runtime.sendMessage to send messages. This custom wrapper makes sure the messages are formatted correctly, are safe, etc.

However there is a lot of turnaround of devs in the codebase and new devs often try to use chrome.runtime.sendMessage directly (and not the wrapper).

Is there any way I can display a warning, either at compile time or in their IDE, when they try to access that method?

答案1

得分: 5

你可以使用类型注解,以下是一个示例,用于避免在 Node.js 中使用 new Buffer(number)

interface BufferConstructor {
    /**
     * 分配 {size} 个八位字节的新缓冲区。
     *
     * @param size 要分配的八位字节的数量。
     * @deprecated 自 v10.0.0 起弃用 - 请改用 `Buffer.alloc()`(还请参阅 `Buffer.allocUnsafe()`)。
     */
    new (size: number): Buffer;
}

如果你使用它,IDE 将会给出警告:
我如何防止开发人员使用全局变量?

英文:

You can use type annotation, below is an example used to avoid new Buffer(number) for Node.js

interface BufferConstructor {
        /**
         * Allocates a new buffer of {size} octets.
         *
         * @param size count of octets to allocate.
         * @deprecated since v10.0.0 - Use `Buffer.alloc()` instead (also see `Buffer.allocUnsafe()`).
         */
        new (size: number): Buffer;
}

If you use it, The IDE will give you a warning:
我如何防止开发人员使用全局变量?

huangapple
  • 本文由 发表于 2023年7月17日 18:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定