当前位置:网站首页>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')
边栏推荐
猜你喜欢

Spark Learning Notes (V) -- spark core core programming RDD conversion operator

数据库概论 - 数据库的介绍

Indexing best practices

Contour detection based on OpenCV (2)
![[learn FPGA programming from scratch -54]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Altera)](/img/4f/f75cfeb4422120ef9ac70cdeb0a840.png)
[learn FPGA programming from scratch -54]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Altera)
![[untitled] JDBC connection database read timeout](/img/24/726ed8b3419866244a1b69e6485d7c.png)
[untitled] JDBC connection database read timeout

Add support for @data add-on in idea

477-82(236、61、47、74、240、93)

复盘:DFS与BFS的主要区别,在思想上的区别,代码实现上的区别

数字孪生应用及意义对电力的主要作用,概念价值。
随机推荐
Code practice when the queue reaches the maximum length
How to design the red table of database to optimize the performance
常见弱口令大全
spark学习笔记(四)——sparkcore核心编程-RDD
Explain tool actual operation
消息被拒MQ
Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial
数据库使用安全策略
优炫数据库集群如何唯一标识一条用户SQL
架构基本概念和架构本质
索引最佳实践
How can you access the domestic server and overseas server quickly with one database?
C语言const用法详解
Introduction to database - Introduction to database
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
DNS记录类型及相关名词解释
Explain详解
30分钟彻底弄懂 synchronized 锁升级过程
Quick sequencing and optimization
复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些