Skip to content

19.扩展全局变量类型

一.扩展局部变量

可以直接使用接口对已有类型进行扩展

ts
interface String {
    double():string
}
String.prototype.double = function () {
    return this as string + this;
}
let str = 'coderlibs';
ts
interface Window {
    mynane:string
}
console.log(window.mynane)

二.模块内全局扩展

ts
declare global{
    interface String {
        double():string;
    }
    interface Window{
        myname:string
    }
}

声明全局表示对全局进行扩展

三.声明合并

同一名称的两个独立声明会被合并成一个单一声明,合并后的声明拥有原先两个声明的特性。

1.同名接口合并

ts
interface Animal {
    name:string
}
interface Animal {
    age:number
}
let a:Animal = {name:'zf',age:10};

2.命名空间的合并

  • 扩展类

    ts
    class Form {}
    namespace Form {
        export const type = 'form'
    }
  • 扩展方法

    ts
    function getName(){}
    namespace getName {
        export const type = 'form'
    }
  • 扩展枚举类型

    ts
    enum Seasons {
        Spring = 'Spring',
        Summer = 'Summer'
    }
    namespace Seasons{
        export let Autum = 'Autum';
        export let Winter = 'Winter'
    }

3.交叉类型合并

ts
import { createStore, Store } from 'redux';
type StoreWithExt = Store & {
    ext:string
}
let store:StoreWithExt

四.生成声明文件

配置tsconfig.json 为true 生成声明文件

ts
"declaration": true