美文网首页
typescript declare的使用

typescript declare的使用

作者: FConfidence | 来源:发表于2019-10-05 16:48 被阅读0次
  1. 模块扩展

    // 1.ts
    export class AClass {
      public a:string;
      constructor(a:string) {
        this.a = a;
      }
     }
    
    // 2.ts
    import { AClass } from './1';
    
    declare module './1' {
       interface AClass {
        test: (b: number) => number;
      }
    }
    
    AClass.prototype.test = (b: number): number => {
       return b;
    }
    
  2. 全局扩展

    // observable.ts
    export class Observable<T> {
        // ... still no implementation ...
    }

    declare global {
        interface Array<T> {
            toObservable(): Observable<T>;
        }
    }

    Array.prototype.toObservable = function () {
        // ...
    }

相关文章

网友评论

      本文标题:typescript declare的使用

      本文链接:https://www.haomeiwen.com/subject/wkfypctx.html