当前位置:网站首页>Typescript declaration merge
Typescript declaration merge
2022-06-11 07:59:00 【YY little monster】
1. Interface merge
interface TestInterface {
name:string;
}
interface TestInterface {
age:number;
}
// It's equivalent to
interface TestInterface {
name:string;
age:number;
}
class Person implements TestInterface{
name:string;
age:number;
}
1.1 If the interface with the same name has the same property name , Then the attribute types must be consistent
interface TestInterface {
name:string;
}
interface TestInterface {
name:number;// Report errors
}
1.2 If a function with the same name appears in the interface with the same name , Then it will become a function overload
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
namespace Validation{
export let name:string = 'lnj';
}
namespace Validation{
export let age:number = 18;
}
console.log(Validation.name);
console.log(Validation.age);
2.1 A variable with the same name cannot appear in a namespace with the same name , Such method
namespace Validation{
export let name:string = 'lnj';
export let say = ()=> "abc";
}
namespace Validation{
export let name:string = 'zs';// Report errors
export let say = ()=> "abc";// Report errors
}
2.2 Other namespaces in the namespace with the same name did not pass export The exported content cannot be obtained
namespace Validation{
let name:string = 'lnj';// Output name = Can't get name
// export let name:string = 'lnj'; Output name =lnj
}
namespace Validation{
export let say = ()=> {
console.log(`name = ${
name}`);
};
}
Validation.say();
3. Namespace and class merge
Be careful : Class must be defined before namespace
The method exported from the namespace will be merged into the class as a static method
class Person {
say():void{
console.log('hello world');
}
}
namespace Person{
export const hi = ():void=>{
console.log('hi');
}
}
console.dir(Person);
4. Namespace and function merge
Be careful : The function must be defined before the namespace
function getCounter() {
getCounter.count++;
console.log(getCounter.count);
}
namespace getCounter{
export let count:number = 0;
}
5. Namespace and enumeration merge
Be careful : There is no requirement of order
enum Gender {
Male,
Female
}
namespace Gender{
export const Yao:number = 666;
}
console.log(Gender);
边栏推荐
- [atcoder2376] black and white tree (game)
- Servlet
- 安卓初中级开发基础知识整理(面试自用)
- Detailed explanation of shift operator and bit operator in C language
- 使用 COCO 数据集训练 YOLOv4-CSP 模型
- Remote office experience | community essay solicitation
- 排序——交换排序
- 134. gas station
- 2022.6.6 extra long growth simulation
- [atcoder2307] tree game
猜你喜欢

About static keyword

Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本

Summary of embedded software interview questions

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

Return in foreach and break in for

2021-11-05 definition of cache

C# 微信上传Form-data

Bubble sorting with C language

Detailed explanation of shift operator and bit operator in C language

安卓初中级开发基础知识整理(面试自用)
随机推荐
【AtCoder2304】Cleaning
How many of the 50 questions about network knowledge can you answer correctly?
Basic use of typescripy class
JSP development model
2021-10-17
TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者
Dameng user management
2022.6.7 special student simulation
Servlet、ServletConfig、ServletContext
Request request object and response response object
860. 柠檬水找零
Remote office experience | community essay solicitation
Dameng database login
Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases
Dameng database startup and shutdown
El expressions and JSTL
图数据库无缝集成Tushare接口
TypeScript-声明合并
Record a murder case caused by ignoring the @suppresslint ("newapi") prompt
[untitled] Weng_ C lesson 1