chst365's blog chst365's blog
首页
  • Git
  • 网络
  • 操作系统
  • 浏览器
  • webpack
  • JavaScript
  • TypeScript
  • 性能
  • 工程化
  • React
  • 编程题
  • React技术揭秘
  • 算法
  • Node
  • 编码解码
  • NodeJS系列
  • Linux系列
  • JavaScript系列
  • HTTP系列
  • GIT系列
  • ES6系列
  • 设计模式系列
  • CSS系列
  • 小程序系列
  • 数据结构与算法系列
  • React系列
  • Vue3系列
  • Vue系列
  • TypeScript系列
  • Webpack系列
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

chst365

DIV工程师
首页
  • Git
  • 网络
  • 操作系统
  • 浏览器
  • webpack
  • JavaScript
  • TypeScript
  • 性能
  • 工程化
  • React
  • 编程题
  • React技术揭秘
  • 算法
  • Node
  • 编码解码
  • NodeJS系列
  • Linux系列
  • JavaScript系列
  • HTTP系列
  • GIT系列
  • ES6系列
  • 设计模式系列
  • CSS系列
  • 小程序系列
  • 数据结构与算法系列
  • React系列
  • Vue3系列
  • Vue系列
  • TypeScript系列
  • Webpack系列
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 浏览器

  • webpack

  • TypeScript

    • 一、类型基础
    • 二、数据类型
    • 三、接口
    • 四、类
    • 五、类与接口的关系
    • 六、泛型
    • 七、类型检查机制
    • 八、高级类型
    • 九、ES6与CommonJS的模块系统
    • 十、使用命名空间
    • 十一、声明合并
    • 十二、如何编写声明文件?
    • 十三、tsconfig配置
    • 十四、编译工具:从ts-loader到Bable
    • 十五、代码检查工具:从TSLint到ESLint
    • 十六、单元测试
    • 十七、TS在react中的使用
    • 什么是TypeScript?
  • 性能

  • 工程化

  • React

  • JavaScript

  • 编程题

  • React技术揭秘

  • 算法

  • 前端
  • TypeScript
chst365
2020-04-05

十一、声明合并

# 十一、声明合并

编译器会把多个地方具有相同声明合并为一个声明 接口之间可以重复定义,在命名空间不可以

interface A {
    x: number;
    y: number;
    // y: string;
    foo (bar: number): number  //5
    foo (bar: 'a'): number // 2
}
interface A {
    y: number;
    foo (bar: string): string;  // 3
    foo (bar: number[]): number[]  // 4
    foo (bar: 'b'): number // 1
}
let e: A = {
    x: 1,
    y: 1,
    foo(bar: any) {
        return bar
    }
}

// 命名空间和函数的声明合并
function Lib() {}
namespace Lib {
    export let version = '1.0'
}
console.log(Lib.version)

// 命名空间和类的声明合并
class F {}
namespace F {
    export let state = 1
}
console.log(F.state)

// 命名空间和枚举的声明合并
enum Color {
    Red,
    Yellow,
    Blue
}
namespace Color {
    export function mix() {}
}
console.log(Color)

// 函数、类和命名空间的声明合并,必须是命名空间的声明需在后,但是枚举和命名空间的声明合并无此要求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#语言#TypeScript
上次更新: 2021/04/22, 00:08:06
十、使用命名空间
十二、如何编写声明文件?

← 十、使用命名空间 十二、如何编写声明文件?→

最近更新
01
面试官
03-27
02
this&指针&作用域&闭包
03-27
03
前端
03-27
更多文章>
Theme by Vdoing | Copyright © 2019-2025 chst365 | 豫ICP备17031889号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式