天天看点

Typescript中声明合并

声明合并

一个标识符可以表示一个值、也可以表示一个类型或者一个命名空间。对于同一个标识符而言,它可以同时表示多个含义。如果同一声明空间内使用的标识符必须唯一。如果同一声明空间内存在同名的声明时,编译器会尝试将所有同名的声明合并为一个声明。如果无法进行声明合并,就会产生编译错误。合并的会按照值对值、类型对类型,命名空间对命名空间的方法合并。

const zss = 5;
interface People {
    name: 'test',
    age: 22
}
namespace Tea {}      
interface X {
    [prop: string]: string;
}
interface X {
    [prop: number]: string;
}
interface People {
    [prop: string]: string;
    [prop: number]: string;
}      

枚举声明合并

enum X {
    Y
}
enum X {
    U = 6,
}
enum X {
    R = 7,
}
let o: X;
o = X.Y;
o = X.U;
o = X.R;      

其余

class People {
    inner: People.Inner = new People.Inner();
}
namespace People {
    export class Inner {}
}