当前位置:网站首页>The difference between TS Gymnastics (cross operation) and interface inheritance
The difference between TS Gymnastics (cross operation) and interface inheritance
2022-07-06 07:29:00 【Jioho_】
TS Artistic Gymnastics &( Crossover operation ) and The difference between interface inheritance
Cross type and Interface inheritance is merged 2 The role of objects , However, there are still many things to pay attention to in actual use
& Cross type
& Also known as Cross type (Intersection types), Through the first demo Get to know
type Todo1 = {
name: string
}
type Todo2 = {
description: string
}
type Todo = Todo1 & Todo2
In the above demo in
Type derivation display :type Todo = Todo1 & Todo2
The actual effect :type Todo = { name:string; description: string }
situation 2
type NumberAndString = string & number // never
type Todo1 = {
id: string
title: string
do(title: string, status: boolean): void
fn(): void
}
type Todo2 = {
id: number
title: string | number
do(title: string, id: number, status: boolean): void
fn(): void
}
type Todo = Todo1 & Todo2
let todo: Todo = {
id: 'string', // Report errors Type 'string' is not assignable to type 'never'.
id: 1234, // Report errors Type 'number' is not assignable to type 'never'.
title: 'title' // Can only be merged into string type
do(title:string,status:boolean){
},
fn(){
}
}
NumberAndString The result of type derivation of is never, Because there is no value that is string again number Of .
and Todo The result of type derivation is the same ,id Equal to any value is wrong ( the reason being that never). about title Come on string & string | number When , In fact, or string
do Because it is a function type , Even if the parameters are different, they cannot be merged ( Errors will be reported when using ), and fn Because the functions are the same , Merge naturally
Come to a conclusion
- & The cross type is 2 Types of Combine . It is what both sides have that can be merged , Different ones will be excluded
- If you exclude the same thing in the end , Then it will change never type ( That is, the type does not exist )
- Built in type (string,number,boolean,unio Joint type ) You can use &
interface( Interface ) Inheritance
Interface inheritance and The biggest difference between cross types is
Cross type can be in Customize type、 The original type (string,number,boolean…) 、 Joint type (string | number)、 Even Interface Cross each other
Interface crossing also follows & The rules of , Like the one below demo,id Still cross for never type
interface ITodo {
id: string
title: string
do(title: string, status: boolean): void
fn(): void
}
interface ITodo2 = {
id: number
title: string | number
do(title: string, id: number, status: boolean): void
fn(): void
}
type Todo = ITodo & ITodo2
var todo: Todo = {
id: 1, // Report errors
title: '',
do() {
} // Report errors
fn(){
}
}
And inheritance You can only inherit the content of another interface from one interface
Interface inheritance , If you encounter the same key name but the value does not match , Then an error will be reported when inheriting , Instead of merging into never
Include the name of the method , Even if you want to rewrite it after inheriting , Parameters must also be consistent with those before inheritance
Use type Or use interface The ultimate problem
type and interface It's all about defining types , Constraint variables . And they all support expanding their own fields
In succession / Expand the level of fields
In everyday use ,type It can be used & Operator 2 individual type The merger of , There are “ Conflict ” The field of will Automatically Be merged into never ( In defining type I didn't feel that this field would never)
and inteface What we use is “ Inherit ”, In the course of inheritance , If there is a conflict , Then when defining the interface, you will immediately perceive
At the level of defining types
type You can easily combine basic types , Form a new type , such as
// Define a timestamp field , The timestamp may be a string , It can also be numbers
type TimeStamp = string | number | bigInt
var time: TimeStamp = new Date().getTime()
And the interface type , You must define a complete interface . Limitations for a single field ,type Is slightly better
Speaking of the end , It is estimated that there is no standard to specify what kind of situation should be used to define the type .
If the development content may be a base modular , Many places need to inherit this base Development , That recommendation interface. because This is very interface , And when others inherit the past, they add / You will know immediately when you modify the field , Oh , The original field base Module defines , If I change it like this, it will conflict
and type Types can also be defined , Personally, I think it's more about defining There is no need for external thing , perhaps Things that are not often inherited and modified
such as TS Artistic Gymnastics , The tool classes you see are basically type Defined , because type You can return {} Can also return to Single field . At most, when developing tool classes Cross reference , I haven't heard of hold Pick Tool class inherits and adds some functions In this case . type Used to define tool classes , Define a single field , It's very suitable , Defining objects also OK( The interface is not flexible when inheriting ) Each has its own characteristics
So choose type / interface, It still depends on the experience accumulated in business scenarios and code writing . When you write too much , You can see at a glance , This one uses xxx Is the most appropriate
Last
Sum it up
- & Character is used to merge 2 individual / Multiple types of “ intersection ”, When there is conflict in the intersection, it will Automatic conversion to never
- & Yes type and interface Can take effect , For this 2 All definitions can be merged
- extend You can only inherit the content of another interface from one interface
- extend When inheriting an interface, if it is of type “ intersection ” There are conflicts , Then you will be prompted when defining the interface ( Report errors ), Will not automatically convert to never
Since said to type and interface, Finally, I feel that thinking based on the above type combination can smoothly lead to what scenes are suitable for use type Or use it interface
When defining a single field / Define tool classes ( Do type gymnastics ) When type Very suitable
Definition base class / The module needs to provide external functions , Provide the interface / Modules will be inherited by other modules in the past when expanding ,interface Naturally, we should not let it go
边栏推荐
- 多线程和并发编程(二)
- Typescript interface properties
- C - Inheritance - polymorphism - virtual function member (lower)
- Fundamentals of C language 9: Functions
- Excel的相关操作
- Go learning --- use reflection to judge whether the value is valid
- 烧录场景下的源代码防泄密方案分享
- SSM学习
- [MySQL learning notes 30] lock (non tutorial)
- 1091: two or three things in childhood (multi instance test)
猜你喜欢

Go learning --- use reflection to judge whether the value is valid

杰理之BLE【篇】
![[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code](/img/93/ec9de907cae4714038bb5f95aba52b.png)
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
![When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]](/img/3e/3d5bff87995b4a9fac093a6d9f9473.png)
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]

Simulation of Michelson interferometer based on MATLAB

Excel的相关操作

You deserve this high-value open-source third-party Netease cloud music player

Sharing of source code anti disclosure scheme under burning scenario

Seriously recommend several machine learning official account

Crawling exercise: Notice of crawling Henan Agricultural University
随机推荐
SSM学习
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
[computer skills]
TypeScript 可索引类型
How to configure GUI guide development environment
Résumé de la structure du modèle synthétisable
First knowledge of OpenGL es learning (1)
word怎么只删除英语保留汉语或删除汉语保留英文
Jerry needs to modify the profile definition of GATT [chapter]
mysql如何合并数据
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
word中把带有某个符号的行全部选中,更改为标题
Typescript void base type
Uni app practical project
TS 类型体操 之 循环中的键值判断,as 关键字使用
杰理之普通透传测试---做数传搭配 APP 通信【篇】
chrome查看页面fps
杰理之AD 系列 MIDI 功能说明【篇】
C # create database connection object SQLite database
2022年Instagram运营小技巧简单讲解