我有一些描述Api电话的类型。例如:
export class RequestType {
prop1: string;
prop2: string;
}
export class ResponseType {
prop3: string;
prop4: string;
}每个请求类型都链接到响应类型。我目前正在做的是定义一个接口IReturn<T>并将它添加到请求类型中:
export interface IReturn<T> {}
export class RequestType implements IReturn<ResponseType> {
prop1: string;
prop2: string;
}然后我有一个服务,我希望有一个方法从请求类型的构造函数中推断请求类型和响应类型:
import { RequestType, IReturn } from './dto';
export class SomeService {
callApi<TRequest extends IReturn<TResponse>, TResponse>(dto: Request) TResponse {
// implementation
}
}然而,当我试图调用该服务时,TypeScript正确地推断了TRequest,但是TResponse被绑定到{}。
// response is a {} and not a ResponseType!!
const response = this.someService.call(requestInstance);我现在有点不知所措。我如何重构服务、接口或dtos以获得请求和响应类型的类型推断?
发布于 2018-06-28 14:30:58
这里有几个问题,第一个问题是,您有未使用的泛型参数,因为类型记录使用了一个结构类型系统--它们几乎被忽略了。您可以在这个常见问题中看到这个文档。第二个问题是,类型记录不会进行类型推断来猜测TResponse,而TRequest extends IReturn<TResponse>只会选择最简单的TResponse,通常是{}。
为了克服这些限制,我们可以首先在IReturn<T>中使用类型参数,例如,我们可以有一个表示T构造函数的字段(但实际上任何用法都可以,甚至是一个虚拟的字段,比如_unusedField: T)。对于第二个问题,我们可以使用一个条件类型从T中提取IReturn<T>
export class ResponseType {
prop3: string;
prop4: string;
}
export interface IReturn<T> { returnCtor : new (...args: any[] ) => T; }
export class RequestType implements IReturn<ResponseType> {
returnCtor = ResponseType;
prop1!: string;
prop2!: string;
}
export class SomeService {
callApi<TRequest extends IReturn<any>>(dto: TRequest) : TRequest extends IReturn<infer U> ? U : never {
return null as any
}
}
const someService = new SomeService;
const requestInstance = new RequestType;
const response = someService.callApi(requestInstance);https://stackoverflow.com/questions/51085204
复制相似问题