Preface: What I want to talk about today is still related to TS. The most common declaration merger in TS: interface merger Before we talk about interface merging, let's talk about declaration merging Declaration Merge: What is declaration merging? In fact, it is easy to understand that declaration merging in TS means that the compiler will merge declarations with the same name into one declaration. The result of the merger: The merged declaration will have the characteristics of two or more original declarations. doubt: In fact, there are several situations to talk about. Today we will talk about one of them. The simplest and most common type of declaration merging is interface merging. 1. Merge interfaceWe just said that "the merged declaration will have the characteristics of two or more original declarations" The same is true for merging interfaces, which puts the members of both parties into an interface with the same name. It should be noted that the members in the interface include function members and non-function members, and the situation is different 1.1 Non-function membersFor example: interface Box { height: number; } interface Box { width: number; } let box: Box = {height: 2, width: 3}; In the above code, two interfaces with the same name However, it should be noted that the members in the above cases are unique. However, if two interfaces declare non-function members with the same name and their types are different, the compiler will report an error. 1.2 Function membersFor the function members inside, each function declaration with the same name will be treated as an overload of this function. And when interface A is merged with a later interface A, the later interface has a higher priority For example, the official example: interface Cloner { clone(animal: Animal): Animal; } interface Cloner { clone(animal: Sheep): Sheep; } interface Cloner { clone(animal: Dog): Dog; clone(animal: Cat): Cat; } Eventually they will be combined into one statement, as follows: interface Cloner { clone(animal: Dog): Dog; clone(animal: Cat): Cat; clone(animal: Sheep): Sheep; clone(animal: Animal): Animal; } Two points to note:
There are exceptions, however: when special function signatures appear. If a parameter in the signature has a type that is a single string literal (i.e. not a union of string literals), it will be promoted to the top of the overload list. This is the end of this article about the most common declaration merging (interface merging) in TS. For more information about interface merging in TS, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: Should the Like function use MySQL or Redis?
>>: Three ways to jump to a page by clicking a button tag in HTML
There is often a lack of understanding of multi-c...
First, install openssh-server in docker. After th...
Table of contents 1. Character Function 1. Case c...
In my past work, the development server was gener...
This article describes how to compile and install...
Achieve results Code html <div class="css...
Zero, Background I received a lot of alerts this ...
Table of contents 1. Enter a directory and create...
Introduction: This article takes the sample proje...
Table of contents Create a layout Add CSS styles ...
Table of contents Preface 1. Understanding with e...
Here is a brief introduction to indexes: The purp...
Table of contents 1. Implementation 2. Problems 3...
Table of contents Written in front router.json Ro...
1Several common character sets In MySQL, the most...