当前位置:网站首页>TS learning (VII): interface and type compatibility of TS
TS learning (VII): interface and type compatibility of TS
2022-07-25 04:14:00 【Snail at the bottom of the well】
The concept of interface
Function of interface
Used to constrain objects 、 function 、 Contract of class ( standard )
contract ( standard ) In the form of :
- API file , Weak standard , For example, before development , The development team leader has defined some development documents in advance , But in the process of development, you may not standardize according to the document , So API Documents are weak Standards
- Code constraints , Strong standards : The standard used in code is interface :interface
Use and definition of interfaces : Interface constraints and type aliases will not participate in the operation
1、 Interface constraint object
interface Interface name { attribute : type , name:sring, } let u: Interface name ={...} 2、 Define function methods
- How to write it 1: In direct writing
1、
interface Method name { ( Parameters : type ): Return type , };2、 There is also a similar way of writing interface, Is to use type aliases Method name {( Parameters : Parameter type ): Return type }
If two {} If there is no member name in it, this bracket represents the delimiter
type Method name = { ( Parameters : type ): Return type , }
- How to write it 2: Use object attributes to define in objects
interface User{ sayHell:()=>void,// How to write it 1 sayBye():void// How to write it 2 }- // This means There must be sayHell This method does not return a value , Otherwise, the report will be wrong
let u: User={ sayHell(){ console.log('hello world')} sayBye()(){ console.log('goodBye world')} }
3、 Interface inheritance Similar to the writing of class inheritance
class Son extends Father {}
Use interface Example , When inheriting, you can redefine the attributes of the parent , But you cannot modify the attribute type of the parent ,
As in the following example A Object properties T1 yes string, When C Object wants to inherit A object , It can also define a T1 attribute ,
But this attribute cannot be different from the parent's attribute type , For example C Object's T1 Attribute change number, The result will be wrong
So since the parent has its own desired attributes , Don't waste time , Define it by yourself , Therefore, a child interface cannot override the members and types of the parent interface
interface A{ T1:string } interface B extends A{ T2:number } interface C extends A,B{ T1:string, T3:boolean } let u:C ={ T1:"4", T2:7, T3:true }
Use Type the alias type, If you want a type alias to be the same as inheritance, you have to use combination , Use “&” Symbolic connection , It is called cross type , Type alias If in C To change T1 The type of , He won't make a mistake ,
But when using assignment is , You'll find that T1 The type of is to merge the two types , So when assigning values, you cannot assign values
type A ={ T1:string } type B ={ T2:number } type C ={ T1:number, T3:boolean } & A & B let u:C ={ T1:???, T2:7, T3:true }
Type compatibility
1 Definition : What is type compatibility
B->A, If the assignment can be completed , be B and A Type compatible ;TypeScript Type compatibility in is based on structural subtypes . A structure type is a way to describe a type using only its members
My understanding is Define an object , You have some characteristics of this object , Then you can be compatible ,
2 Discrimination method : Duck identification ( Substructure identification method )
The target type requires some characteristics , As long as the type of assignment can meet this feature
3 Type compatible
Basic types : perfect match , There's nothing to say
object type : Duck identification
interface Duck { sound:' Gaga ',// This is the type , Not assignment swim():void,// Can swim } let person ={ name:' People disguised as ducks ', age:18, // sound:' Gaga ',// Here are some small problems , Here we belong to assignment , Instead of literal type , So here we have to use “ Assertion ”, Use as sound:' Gaga ' as " Gaga ", // " The front is the data " as " And then there's the type " swim(){ console.log(this.name + " I'm swimming , And sent out " +this.sound + " The sound of ") } } // This can be assigned normally , because person He has Duck Of sound and swim These two properties , Think it's a duck let duck: Duck =personA special case : If we directly use object literal assignment , You're going to report a mistake .TS Made a more rigorous judgment , because Duck Only sound and swim These two properties ,
So there can only be sound and swim These two properties , Other attributes cannot exist , If it exists, it will report an error , We just need to delete the rest of the implementation
interface Duck { sound:' Gaga ',// This is the type , Not assignment swim():void,// Can swim } // This will give you an error let duck: Duck ={ name:' People disguised as ducks ', age:18, // sound:' Gaga ',// Here are some small problems , Here we belong to assignment , Instead of literal type , So here we have to use “ Assertion ”, Use as sound:' Gaga ' as " Gaga ", // " The front is the data " as " And then there's the type " swim(){ console.log(this.name + " I'm swimming , And sent out " +this.sound + " The sound of ") } } // Correct assignment let duck: Duck ={ sound:' Gaga ' as " Gaga ", // " The front is the data " as " And then there's the type " swim(){ console.log(" I'm swimming , And sent out " +this.sound + " The sound of ") } }
边栏推荐
- Postgraduate entrance examination experience
- [understanding of opportunity-47]: Guiguzi - Chapter 11 - decision makers, moderation, and rational distribution of interests
- Detailed explanation of kubernetes network plug-in - flannel chapter
- Shell string
- JS absolute minimum value of the sum of Huawei od two numbers
- Uniapp picker select the effect demo of provinces and cities (finishing)
- [Flink] protocol operator reduce
- Use "display: flex; justify content: Center; align items: Center;" Solve the problem that the streaming grid layout cannot be centered
- DIY can decorate the mall system, you can also have!
- Implementing DDD based on ABP -- domain logic and application logic
猜你喜欢

DNS resolution experiment

DNS domain name resolution

基于ABP实现DDD--领域逻辑和应用逻辑

EMQ Yingyun technology was successfully selected into the 2022 "cutting edge 100" list of Chinese entrepreneurs

Roleapp focuses on Web3 and opens the door of Web3 with the key of "data can be held"

Pytorch deep learning practice lesson 8 importing data

Original | ueditor1.4.3-asmx bypasses WAF

ES (8.1) certification topic

Emergency response stack

Analytic hierarchy process of MATLAB
随机推荐
Top101 [linked list] must be applied for interview
[Flink] rich function
Max matrix and JS
The latest Matlab download and installation tutorial (with files)
Behind Senma's LP, "Wenzhou Department" is running into the venture capital circle
Original | ueditor1.4.3-asmx bypasses WAF
Niuke interview high frequency list (group 1) difficulty: simple & medium
Method of setting document comments in idea (graphic version)
Apache Doris series: in depth understanding of real-time analytical database Apache Doris
Display: the function and effect of align content, justify content and align items under flex
[Flink] submit the jar package to the Flink cluster and run it
DNS domain name resolution service
DIY can decorate the mall system, you can also have!
The sixth day of brushing questions with force deduction
To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
GDAL image resampling
Which securities company do retail investors choose for stock speculation? Is it safe to open an account on your mobile phone
Advanced numbers | [calculus of multivariate function] chain derivation method, implicit function derivation method, total differential form invariance, multivariate function extremum
Debezium series: optimize cluster parameters and support personalized settings of debezium connector
基于SSM实现后勤报修系统