[英]How to write a class which can implement an interface in TypeScript
我是 TypeScript 的初学者。 我有下面的一段代码
async function sleep(ms: number) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms)
})
}
async function randomDelay() {
const randomTime = Math.round(Math.random() * 1000)
return sleep(randomTime)
}
class ShipmentSearchIndex {
async updateShipment(id: string, shipmentData: any) {
const startTime = new Date()
await randomDelay()
const endTime = new Date()
console.log(`update ${id}@${
startTime.toISOString()
} finished@${
endTime.toISOString()
}`
)
return { startTime, endTime }
}
}
// Implementation needed
interface ShipmentUpdateListenerInterface {
receiveUpdate(id: string, shipmentData: any)
}
我必须编写一个实现ShipmentUpdateListenerInterface
的类。 我怎样才能在 TypeScript 中做到这一点?
使您的类使用 implements 关键字实现接口:
class ShipmentUpdate implements ShipmentUpdateListenerInterface {
receiveUpdate(id: string, shipmentData: any){
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.