在 TypeScript 中,如何使用泛型提取 class 实例类型?

In TypeScript, how can you extract a class instance type using generics?

四处寻找答案,但结果很枯燥——在我的 GraphQL 服务器中,我试图在我的订阅中添加更好的类型,因为我从 NPM 中提取的类型使用 any 太多了.我写了一张概述我的订阅的地图,其中每个地图值都包含频道名称、订阅名称和一个用于键入有效负载的对象 class:

class ConversationCreatedEvent {
  conversationID: string;
  userID: string;
}

export const Subscriptions = {
  conversationCreated: {
    name: 'onConversationCreated',
    event: ConversationCreatedEvent,
    channelName: 'CONVERSATION_CREATED',
  },
};

我想编写一个方法,允许您发布带有两个参数的订阅:订阅 ID(映射键)和符合 class 类型实例的对象 event 字段.

到目前为止,我有:

const publishSubscription = <T extends keyof typeof Subscriptions>(
  name: T,
  data: typeof Subscriptions[T]['event']
) => {
 // TODO: implement
};

但是,这种方法使第二个参数成为 typeof ConversationCreatedEvent,并需要诸如 lengthnameapply 等键(即您的键d 在 class 对象中找到,而不是 class 的实例)。我需要这个来接受实际的 class 成员,例如 conversationID.

希望这一切都有意义。我觉得这很简单,只是缺少一些小东西。我仍在学习映射类型。

感谢任何帮助!!

const Subscriptions 具有 class 构造函数作为值。所以:

typeof Subscriptions['consoversationCreated']['event']

等同于:

typeof ConversationCreatedEvent

这意味着您有一个 class 构造函数类型,并且您想从中获取实例类型。

Typescript 有一个内置的实用程序类型可以做到这一点 called InstanceType.

class Foo {}

// The following type aliases are equivalent
type FooInstance = Foo
type InstanceTypeFoo = InstanceType<typeof Foo>

或者您的情况:

InstanceType<typeof Subscriptions[T]['event']>

See playground