有没有办法在typescript文件中创建一个定义全局可访问类型的文件?
我喜欢typescript,但我发现,当我想真正做到类型安全时,我必须显式地从整个系统导入类型.这很烦人.
有没有办法在typescript文件中创建一个定义全局可访问类型的文件?
我喜欢typescript,但我发现,当我想真正做到类型安全时,我必须显式地从整个系统导入类型.这很烦人.
是的,这是可能的.你可以在这里找到所有信息:https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html
重要的是:
declare global {
/*~ Here, declare things that go in the global namespace, or augment
*~ existing declarations in the global namespace
*/
interface String {
fancyFormat(opts: StringFormatOptions): string;
}
}