Use when extending third-party types. Use when adding properties to existing interfaces. Use when plugins extend core types. Use when declaration merging is needed. Use when augmenting global types.
Module augmentation allows you to add declarations to existing modules, including third-party libraries. This is useful when a library's types are incomplete or when you're extending a library with plugins. Use declare module to add properties, methods, or types to existing modules.
Use module augmentation to add to existing types. Declare the same module name and add your declarations.
// Adding to an existing module
// types/vue.d.ts
declare module 'vue' {
export interface ComponentCustomProperties {
$myProperty: string;
$myMethod(): void;
}
}
// Now Vue components have these properties
// this.$myProperty works with type safety
// Augmenting global types
declare global {
interface Window {
myLib: MyLibrary;
}
}
// Now window.myLib is typed
PyTorch深度学习模式与最佳实践,用于构建稳健、高效且可复现的训练流程、模型架构和数据加载。