18. TypeScript 扩展全局变量类型

TypeScript 扩展全局变量类型

1. 扩展局部变量

// 可以直接使用接口对已有类型进行扩展
interface String {
  double():string
}
String.prototype.double = function () {
  return this as string + this;
}
let str = '123';

interface Window {
  mynane:string
}
console.log(Window.mynane)

2. 模块内全局扩展

declare global{
  interface String {
      double():string;
  }
  interface Window{
      myname:string
  }
}
// 声明全局表示对全局进行扩展

3. 声明合并

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

// 同名接口合并
interface Animal {
  name:string
}
interface Animal {
  age:number
}
let a:Animal = {name:'456',age:10};


// 命名空间的合并

// 扩展类
class Form {}
namespace Form {
    export const type = 'form'
}

// 扩展方法
function getName(){}
namespace getName {
    export const type = 'form'
}

// 扩展枚举类型
enum Seasons {
  Spring = 'Spring',
  Summer = 'Summer'
}
namespace Seasons{
  export let Autum = 'Autum';
  export let Winter = 'Winter'
}

3. 交叉类型合并

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

4. 生成声明文件

配置tsconfig.json 中 "declaration": true 生成声明文件
GitHubGitee
https://github.com/shunyue1320/TypeScript-learnhttps://gitee.com/shunyue/TypeScript-learn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值