如何在编译时在Typescript中检查扩展函数

如何在编译时在Typescript中检查扩展函数,typescript,compiler-errors,extension-methods,Typescript,Compiler Errors,Extension Methods,我正在寻找一种在编译时失败的方法,如果开发人员在使用扩展函数时没有导入实现文件 文件string.d.ts export {}; declare global { interface String { padZero (length: number): string; } } 文件string.extensions.ts String.prototype.padZero = function (this: string, length: number): st

我正在寻找一种在编译时失败的方法,如果开发人员在使用扩展函数时没有导入实现文件

文件string.d.ts

export {};

declare global {
    interface String {
        padZero (length: number): string;
    }
}
文件string.extensions.ts

String.prototype.padZero = function (this: string, length: number): string {
   ... implementation
}
代码中的任何地方

'TEST'.padZero(10); // Fails at runtime because "import '...string.extensions.ts';" was forgotten
如何确保在编译时正确完成导入


编辑:我没有找到一种方法,但是该应用只需要导入一次,因此您可以在app.module.ts中进行导入,例如,声明对类型很好,但不能保证代码正常工作。您应该在代码中进行测试,以确保功能正常工作。我不想测试padZero的实现,我想避免任何运行时错误,因为有人忘记了导入(如果可能的话)