繁体   English   中英

Typescript:通过检查字符串值推断通用类型

[英]Typescript: Infer generic type by checking string value

我有这样的类型:

export type NotificationType = 'comment' | 'like' | 'message' | 'follow' | unknown

type MessageBody<T extends NotificationType> = T extends 'comment'
  ? {
      body: string
      threadId: string
      projectId: string
    }
  : T extends 'like'
  ? {
      resourceId: string
      resourceType: 'project' | 'projectVersion' | 'thread' | 'comment'
    }
  : T extends 'message'
  ? {
      body: string
      chatId: string
    }
  : T extends 'follow'
  ? {}
  : never

export type Message<T extends NotificationType = unknown> = {
  channel: string
  message: MessageBody<T> & {
    type: T
  }
  timetoken: string | number
  uuid?: string
  publisher?: string
}

我现在想做的是通过检查属性message.type来推断T的类型,而不必事先指定T的类型,就像这样:

function checkMessage(message: Message) { // No generic type given
  switch (message.message.type) {
    case 'comment':
      message.message.body // infers to MessageBody<'comment'>
  }
}

但是通过这种方法,我在 switch 语句中收到错误消息: Property 'type' does not exist on type 'never'.

这可能来自NotificationType中对unknown的使用。 我怎样才能拥有通用可选并仍然通过检查字符串来推断类型?

我敢打赌NotificationType应该只是一个字符串。 因此,使用unknown并不是表示未知字符串类型的最佳选择。 最好只使用 refular string类型。 但是,将string类型与其他文字字符串(如commentfollow )一起使用会将它们冲突为string 这就是为什么 t 最好使用string & {}的原因。

另外,我使用了 map 数据结构而不是条件类型MessageBody

export type KnownType = 'comment' | 'like' | 'message' | 'follow'

export type UnknownType = string & {}

type NotificationType = KnownType | UnknownType

type MessageMap = {
  comment: {
    body: string
    threadId: string
    projectId: string
  },
  like: {
    resourceId: string
    resourceType: 'project' | 'projectVersion' | 'thread' | 'comment'
  },
  message: {
    body: string
    chatId: string
  },
  follow: {}
}

type MessageBase = {
  channel: string
  timetoken: string | number
  uuid?: string
  publisher?: string
}

type MessageBody<T extends KnownType> = { message: MessageMap[T] & { type: T } } & MessageBase

export type ComputeMessage<T extends NotificationType> = T extends KnownType ? MessageBody<T> : never

function checkMessage<MessageType extends NotificationType>(message: ComputeMessage<MessageType>) {

  switch (message.message.type) {
    case 'comment':
      message.message.projectId // ok
      message.message.threadId // ok
  }
}

操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM