Typescript 如何键入基于第一个参数类型的扩展泛型类型的泛型函数?

Typescript 如何键入基于第一个参数类型的扩展泛型类型的泛型函数?,typescript,generics,types,Typescript,Generics,Types,我有以下定义: class RESTClient<E extends { id: number }> extends Client {} class ParticipantClient extends RESTClient<Participant> {} const createRESTRequestAction = <E extends { id: number }>(client: RESTClient<E>, value?: E) =>

我有以下定义:

class RESTClient<E extends { id: number }> extends Client {}
class ParticipantClient extends RESTClient<Participant> {}
const createRESTRequestAction = <E extends { id: number }>(client: RESTClient<E>, value?: E) => {};
。。TypeScript要求
someRecord
的类型为
{id:number}
。在这种情况下,我希望
E
类型为
Participant
。如何做到这一点

是否可以推断扩展RESTClient的泛型类型


提前谢谢你

在这种情况下,函数本身是通用的。调用函数时缺少类型参数。像这样调用它:
createRestrequesAction(ParticipantClient,someRecord)

下面是一个工作示例(稍微清理):

类RestObject{
身份证号码
}
类参与者扩展RestObject{
名称:字符串;
}
类RESTClient{}
类ParticipantClient扩展RESTClient{}
const createRestrequesAction=(客户端:RESTClient,值?:T)=>{};
const participantClient=new participantClient();
const anyObj={};
常量otherRestObject={
身份证号码:1
}
常数参与者:参与者={
id:10,
名字:“罗宾”
}
createRESTRequestAction(participantClient,anyObj);//TypeError,{}不可分配给参与者
createRESTRequestAction(participantClient,otherRestObject);//TypeError,otherRestObject参数中缺少“Name”属性
createRESTRequestAction(participantClient,otherRestObject);//工作时,transpiler希望第二个对象扩展RestObject
createRESTRequestAction(participantClient,participant);//作品

在这种情况下,函数本身是通用的。调用函数时缺少类型参数。像这样调用它:
createRestrequesAction(ParticipantClient,someRecord)

下面是一个工作示例(稍微清理):

类RestObject{
身份证号码
}
类参与者扩展RestObject{
名称:字符串;
}
类RESTClient{}
类ParticipantClient扩展RESTClient{}
const createRestrequesAction=(客户端:RESTClient,值?:T)=>{};
const participantClient=new participantClient();
const anyObj={};
常量otherRestObject={
身份证号码:1
}
常数参与者:参与者={
id:10,
名字:“罗宾”
}
createRESTRequestAction(participantClient,anyObj);//TypeError,{}不可分配给参与者
createRESTRequestAction(participantClient,otherRestObject);//TypeError,otherRestObject参数中缺少“Name”属性
createRESTRequestAction(participantClient,otherRestObject);//工作时,transpiler希望第二个对象扩展RestObject
createRESTRequestAction(participantClient,participant);//作品

谢谢您的回答!没有办法自动推断泛型类型吗?因此,我在调用
createRestrequesAction
时不必指定它。据我所知,如果不使用类型参数,就无法从另一个参数推断出一个参数的类型。谢谢您的回答!没有办法自动推断泛型类型吗?因此,在调用
createRestrequesAction
时,我不必指定它。据我所知,如果不使用类型参数,就无法从一个参数推断另一个参数的类型。
createRESTRequestAction(ParticipantClient, someRecord);
class RestObject {
    id: number
}

class Participant extends RestObject {
    name: string;
}

class RESTClient<E extends RestObject> {}
class ParticipantClient extends RESTClient<Participant> {}
const createRESTRequestAction = <T extends RestObject>(client: RESTClient<T>, value?: T) => {};

const participantClient = new ParticipantClient();

const anyObj = {};

const otherRestObject = {
    id: 1
}

const participant: Participant = {
    id: 10,
    name: 'Robin'
}

createRESTRequestAction<Participant>(participantClient, anyObj); // TypeError, {} not assignable to Participant

createRESTRequestAction<Participant>(participantClient, otherRestObject); // TypeError, "Name" property is missing in otherRestObject parameter

createRESTRequestAction(participantClient, otherRestObject); // Works, transpiler expects the second object to extend RestObject

createRESTRequestAction<Participant>(participantClient, participant); // Works!