当前位置:网站首页>TypeScript-声明合并
TypeScript-声明合并
2022-06-11 07:54:00 【YY小怪兽】
1.接口合并
interface TestInterface {
name:string;
}
interface TestInterface {
age:number;
}
//相当于下面
interface TestInterface {
name:string;
age:number;
}
class Person implements TestInterface{
name:string;
age:number;
}
1.1同名接口如果属性名相同, 那么属性类型必须一致
interface TestInterface {
name:string;
}
interface TestInterface {
name:number;//报错
}
1.2同名接口如果出现同名函数, 那么就会成为一个函数的重载
interface TestInterface {
getValue(value:number):number;
}
interface TestInterface {
getValue(value:string):number;
}
let obj:TestInterface = {
getValue(value:any):number{
if(typeof value === 'string'){
return value.length;
}else{
return value.toFixed();
}
}
}
console.log(obj.getValue("abcdef"));
console.log(obj.getValue(3.14));
2.命名空间
namespace Validation{
export let name:string = 'lnj';
}
namespace Validation{
export let age:number = 18;
}
console.log(Validation.name);
console.log(Validation.age);
2.1同名的命名空间中不能出现同名的变量,方法等
namespace Validation{
export let name:string = 'lnj';
export let say = ()=> "abc";
}
namespace Validation{
export let name:string = 'zs';//报错
export let say = ()=> "abc";//报错
}
2.2同名的命名空间中其它命名空间没有通过export导出的内容是获取不到的
namespace Validation{
let name:string = 'lnj';//输出name = 获取不到name
// export let name:string = 'lnj'; 输出name =lnj
}
namespace Validation{
export let say = ()=> {
console.log(`name = ${
name}`);
};
}
Validation.say();
3.命名空间和类合并
注意点: 类必须定义在命名空间的前面
会将命名空间中导出的方法作为一个静态方法合并到类中
class Person {
say():void{
console.log('hello world');
}
}
namespace Person{
export const hi = ():void=>{
console.log('hi');
}
}
console.dir(Person);
4.命名空间和函数合并
注意点: 函数必须定义在命名空间的前面
function getCounter() {
getCounter.count++;
console.log(getCounter.count);
}
namespace getCounter{
export let count:number = 0;
}
5.命名空间和枚举合并
注意点: 没有先后顺序的要求
enum Gender {
Male,
Female
}
namespace Gender{
export const Yao:number = 666;
}
console.log(Gender);
边栏推荐
猜你喜欢

零基础自学SQL课程 | OUTER JOIN外连接

Paging of the flask page

Detailed explanation of character function and string function (including simulation implementation)

空间几何

Data visualization and Matplotlib

图数据库无缝集成Tushare接口

Zero foundation self-study SQL course | outer join external connection

Collation of basic knowledge of intermediate development of Andrews (for interview)

A detailed explanation of one of the causes of dead loop caused by array out of bounds in C language

2022.6.7 特长生模拟
随机推荐
排序——选择排序
multi-sig SC
[atcoder1984] wide swap
multi-sig SC
[codeforces1019e] raining season
【AtCoder2387】+/- Rectangle
The solution of "no startup device" after running Bochs
Classes and objects (medium)
[untitled] Weng_ C lesson 1
Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np
[atcoder1980] mystious light (mathematical simulation)
Classes and objects (Part 2)
C language to achieve a simple game - minesweeping
Black Qunhui dsm7.0.1 physical machine installation tutorial
零基础自学SQL课程 | OUTER JOIN外连接
C language - growth diary-04- preliminary exploration of local variables (local variables)
Request request object and response response object
Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases
Rabin Miller prime test
Return in foreach and break in for