当前位置:网站首页>Typescript TS basic knowledge interface, generics
Typescript TS basic knowledge interface, generics
2022-07-27 03:35:00 【Tangtang 246】
One 、 Interface
Interface is used to define an object structure , Used to define which attributes and methods should be included in an object
Use keywords interface Definition
interface PersonObj{
name: string
age:number
}
const obj:PersonObj = {
name: ' Xiao Ming ',
age: 18,
// gender: ' male ', // Report errors ,gender Not in the PersonObj In the definition of
}
1. Interfaces can also be used as type declarations , Same as type
type MyType{
name: string,
age: number
}
const obj:MyType = { ... }
Difference :
type Cannot declare a type with the same name twice , Will report a mistake
type MyType{ ... }
type MyType{ ... } // Report errors
Interface interface You can declare the type of the same name twice , The result is a combination of two statements , But if there are previously declared attributes in subsequent declarations , Then the attribute type must be consistent
interface PersonObj{
name: string
age:number
}
interface PersonObj{
gender:string
}
const obj:PersonObj = {
name: ' Xiao Ming ',
age: 18,
gender: ' male '
}
2. Implement the interface with class
Interfaces can be used when defining objects Restrict the structure of the object , All properties in an interface cannot have actual values , namely Interfaces only define object structures , Regardless of the actual value , Similar to abstract classes , The difference is that all properties and methods in the interface are abstract , Abstract classes can have substantial properties and methods
When defining a class , You can make a class implement an interface ( Make the class meet the requirements of the interface ), Use keywords implements
interface MyInter{
name:string
sayHi():void
}
class MyClass implements MyInter{
name:string
constructor(name:string){
this.name = name
}
sayHi(): void {
console.log('Hi~')
}
}
Two 、 Generic
When defining a function or class , Encountered an ambiguous type , You can use generics , A generic type is an indefinite type , The specific type is passed in when calling
1. Specify a generic type
function fn<T>(a: T): T{ return a }
// You can call functions with generics directly
fn(10) // Do not specify generics ,TS Types can be inferred automatically
fn<string>('hello') // Specifying generics
2. Specify multiple generics
function fn<T, K>(a:T, b:K):T{
return a
}
fn(10, 'hello')
fn<number,string>(10,'hello')
3. Limit the scope of generics
T extends Inter For generics T Must be Inter Implementation class ( Subclass )
interface Inter{
length: number
}
function fn<T extends Inter>(a:T):number{
return a.length
}
fn('hello')
class MyClass<K>{
name: K
constructor(name: K){
this.name = name
}
}
const myClass = new MyClass<string>('a')
边栏推荐
- A new paradigm of distributed deep learning programming: Global tensor
- Contour detection based on OpenCV (2)
- Spark: ranking statistics of regional advertising hits (small case)
- 图解 SQL,这也太形象了吧!
- Mysql: summary of common sub database and sub table schemes of Internet companies
- Indexing best practices
- MySQL中文失败问题
- 【1206. 设计跳表】
- 一种分布式深度学习编程新范式:Global Tensor
- Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)
猜你喜欢
随机推荐
How to uniquely identify a user SQL in Youxuan database cluster
技术风向标 | 云原生技术架构成熟度模型解读
阶乘末尾0的数量
spark:计算不同分区中相同key的平均值(入门级-简单实现)
再学RecyclerView的回收复用机制
spark学习笔记(五)——sparkcore核心编程-RDD转换算子
架构基本概念和架构本质
Byte side: can TCP and UDP use the same port?
Graphic SQL, this is too vivid!
Details of impala implementation plan
C语言const用法详解
Mysql database related operations
typescript ts 基础知识之接口、泛型
Explain tool actual operation
30 minutes to thoroughly understand the synchronized lock upgrade process
768. 最多能完成排序的块 II 贪心
opiodr aborting process unknown ospid (21745) as a result of ORA-609
shell awk
带你了解什么是 Web3.0
在typora中插入图片和视频








