老师,类型可以在最开始的时候定义好,调用跟赋值的时候不需要再定义了
来源:3-2 TS 函数类型复杂实战:手写 Promise 开头两段源码

学习爱我吖
2024-04-18
// 这里只是初步了解Promise底层源码的代码片段
type ResolveFnType = (resolve_success: any) => any;
type RejectFnType = (reject_fail: any) => any;
type FnType = (resolve: ResolveFnType, reject: RejectFnType) => any;
class Promise {
public resolveFunc!: ResolveFnType
public rejectFunc!: RejectFnType
constructor(promise: FnType) {
this.resolveFunc = (resolve_success) => {
console.log(resolve_success);
};
this.rejectFunc = (reject_fail) => {
console.log(reject_fail);
};
promise(this.resolveFunc, this.rejectFunc)
}
}
new Promise((resolve, reject) => {
resolve('成功了')
reject('失败了')
})
export {}
写回答
1回答
-
keviny79
2025-04-19
可以提上去,没提是为了授课看的更直观一些
00
相似问题