首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型化约束下的TypeScript推理

类型化约束下的TypeScript推理
EN

Stack Overflow用户
提问于 2018-06-28 14:17:35
回答 1查看 358关注 0票数 2

我有一些描述Api电话的类型。例如:

代码语言:javascript
运行
复制
export class RequestType {
  prop1: string;
  prop2: string;
}

export class ResponseType {
  prop3: string;
  prop4: string;
}

每个请求类型都链接到响应类型。我目前正在做的是定义一个接口IReturn<T>并将它添加到请求类型中:

代码语言:javascript
运行
复制
export interface IReturn<T> {}

export class RequestType implements IReturn<ResponseType> {
  prop1: string;
  prop2: string;
}

然后我有一个服务,我希望有一个方法从请求类型的构造函数中推断请求类型和响应类型:

代码语言:javascript
运行
复制
import { RequestType, IReturn } from './dto';

export class SomeService {
    callApi<TRequest extends IReturn<TResponse>, TResponse>(dto: Request) TResponse {
      // implementation
    }
}

然而,当我试图调用该服务时,TypeScript正确地推断了TRequest,但是TResponse被绑定到{}

代码语言:javascript
运行
复制
// response is a {} and not a ResponseType!!
const response = this.someService.call(requestInstance);

我现在有点不知所措。我如何重构服务、接口或dtos以获得请求和响应类型的类型推断?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-28 14:30:58

这里有几个问题,第一个问题是,您有未使用的泛型参数,因为类型记录使用了一个结构类型系统--它们几乎被忽略了。您可以在这个常见问题中看到这个文档。第二个问题是,类型记录不会进行类型推断来猜测TResponse,而TRequest extends IReturn<TResponse>只会选择最简单的TResponse,通常是{}

为了克服这些限制,我们可以首先在IReturn<T>中使用类型参数,例如,我们可以有一个表示T构造函数的字段(但实际上任何用法都可以,甚至是一个虚拟的字段,比如_unusedField: T)。对于第二个问题,我们可以使用一个条件类型从T中提取IReturn<T>

代码语言:javascript
运行
复制
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);

操场链接

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51085204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档