当前位置:网站首页>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);
边栏推荐
- C language - Growth Diary -03- function definition and function prototype declaration
- [IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
- Storage of floating point in memory
- 运筹学导论
- 2022.6.6 extra long growth simulation
- 零基础自学SQL课程 | OUTER JOIN外连接
- Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np
- JSP technology: JSP overview, JSP basic syntax, JSP instructions, JSP implicit objects, JSP action elements
- Switch statement
- ConstraintLayout中使用Guideline限制控件最大宽度
猜你喜欢

Magnifying mirror rendering

About static keyword

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

【案例解读】医疗单据OCR识别助力健康险智能理赔

Alchemy experience (model training of deep learning) the necessity of timely adjusting training parameters for some situations (the adjustment of learning rate LR is the primary) summarizes some metho

Using Tkinter to realize guessing numbers game

Paging of the flask page

C language - growth diary-04- preliminary exploration of local variables (local variables)

用 Keras/TensorFlow 2.9 创建深度学习模型的方法总结

Import on CSDN MD file
随机推荐
3年功能测试拿8K,被新来的反超,其实你在假装努力
Classes and objects (Part 2)
Euler's theorem and its extension (with proof)
Sort - merge sort
ConstraintLayout中使用Guideline限制控件最大宽度
避免list的并发修改异常的几种方式
multi-sig SC
零基础自学SQL课程 | OUTER JOIN外连接
[cluster] haproxy load balancing
TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者
Detailed explanation of character function and string function (including simulation implementation)
Bladed入門教程(視頻)
Batch splice string
2021-10-17
Clipping and overlapping of YUV data
Zero foundation self-study SQL course | union joint query
Simple configuration of vscade
2022.6.7 special student simulation
Introduction to operations research
Data visualization and Matplotlib