前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TypeScript-声明合并

TypeScript-声明合并

原创
作者头像
杨不易呀
发布2023-09-29 20:34:58
2150
发布2023-09-29 20:34:58
举报
文章被收录于专栏:杨不易呀杨不易呀

在 ts 当中接口和命名空间是可以重名的, ts 会将多个同名的合并为一个

接口

代码语言:typescript
复制
interface TestInterface {
    name: string;
}

interface TestInterface {
    age: number;
}

class Person implements TestInterface {
    name: string;
    age: number;
}

let person = new Person();
person.name = "yangbuyiya";
person.age = 18;
console.log(person);
  • 同名接口如果属性名相同, 那么属性类型必须一致
image-20211205210206118
image-20211205210206118
代码语言:typescript
复制
interface TestInterface {
    name: string;
}

interface TestInterface {
    name: number;
}
  • 同名接口如果出现同名函数, 那么就会成为一个函数的重载
代码语言:typescript
复制
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));

命名空间

代码语言:typescript
复制
namespace Validation {
    export let name: string = 'yangbuyiya';
}
namespace Validation {
    export let age: number = 18;
}
console.log(Validation.name);
console.log(Validation.age);
  • 同名的命名空间中不能出现同名的变量, 方法等
image-20211205210347738
image-20211205210347738
代码语言:typescript
复制
namespace Validation {
    export let name: string = 'yangbuyiya';
    export let say = () => "abc";
}
namespace Validation {
    export let name: string = 'zs';
    export let say = () => "abc";
}
  • 同名的命名空间中其它命名空间没有通过 export 导出的内容是获取不到的
代码语言:typescript
复制
namespace Validation {
    let name: string = 'yangbuyiya';
}
namespace Validation {
    export let say = () => {
        console.log(`name = ${name}`);
    };
}
Validation.say();
image-20211205210424361
image-20211205210424361

除了同名的接口和命名空间可以合并以外 命名空间还可以和同名的 类/函数/枚举 合并

命名空间和类合并

类必须定义在命名空间的 前面 会将命名空间中导出的方法作为一个 静态方法 合并到类中

image-20211205211158451
image-20211205211158451
代码语言:typescript
复制
class Person {
    say(): void {
        console.log('hello world');
    }
}

namespace Person {
    export const hi = (): void => {
        console.log('hi');
    }
}
console.dir(Person);

命名空间和函数合并

函数必须定义在命名空间的 前面

代码语言:typescript
复制
function getCounter() {
    getCounter.count++;
    console.log(getCounter.count);
}

namespace getCounter {
    export let count: number = 0;
}

getCounter();

命名空间和枚举合并

没有 先后顺序 的要求

代码语言:typescript
复制
enum Gender {
    Male,
    Female
}

namespace Gender {
    export const Yao: number = 666;
}
console.log(Gender);
输入图片说明
输入图片说明

最后

本期结束咱们下次再见?~

? 关注我不迷路,如果本篇文章对你有所帮助,或者你有什么疑问,欢迎在评论区留言,我一般看到都会回复的。大家点赞支持一下哟~ ?

我正在参与2023腾讯技术创作特训营第二期有奖征文,瓜分万元奖池和键盘手表

输入图片说明
输入图片说明

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 接口
  • 命名空间
  • 命名空间和类合并
  • 命名空间和函数合并
  • 命名空间和枚举合并
  • 最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com