我想使用泛型来定义承诺返回值,当我调用get方法时,我定义了IReq和IRsp,但是IRsp(U)似乎是错误的。
const httpTest = {
get<T, U>(options: T) {
return new Promise<U>((resolve, reject) => {
// async
setTimeout(() => {
const res = { status: '', data: {} };
resolve(res);
}, 2000);
});
},
};
interface IReq {
url: string;
data: {
[propName: string]: any;
};
}
interface IRsp<K> {
status: string;
data?: K;
}
httpTest.get<IReq, IRsp<{ name: string; age: number }>>({
url: '/api/v2/config/get',
data: {
key: 'test',
},
});