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 生成声明文件