【问题标题】:Correct TS declaration for mapping fields together将字段映射在一起的正确 TS 声明
【发布时间】:2021-07-08 15:38:03
【问题描述】:

我有一个AccountDefinition,看起来像这样:

something: {
  type: 'client',
  parameters: {
    foo: 3
  }
},
other: {
  type: 'user',
  parameters: {
    bar: 3
  }
},
...

TS 声明工作正常,但我现在正在尝试创建一个“生成器”(doThings) 函数,并遇到了如何正确键入它的挑战。我也愿意重构所有这些类型。

export interface Spec {
  type: `${SpecType}`
  parameters: unknown
}

export interface UserSpec extends Spec {
  type: `${SpecType.USER}`
  parameters: UserSpecParameters
}

export interface ClientSpec extends Spec {
  type: `${SpecType.CLIENT}`
  parameters: ClientSpecParameters
}

export interface AccountDefinition {
  [k: string]: UserSpec | ClientSpec
}

export enum SpecType {
  USER = 'user',
  CLIENT = 'client'
}

export type SpecParametersMap = {
  user: {
    bar?: number
  }
  client: ClientSpecParameters
}

export interface UserSpecParameters {
  bar?: number
}

export interface ClientSpecParameters {
  foo: number
}

export const doThing = <T extends SpecType>( // Ideally not a generic if it can infer parameters from type
  type: T,
  parameters: SpecParametersMap[T]
): void => {
  const account: AccountDefinition = {
    // Example
    foo: {
      type: 'client',
      parameters: {
        foo: 3
      }
    },
    // TS Error:
    // Type '{ parameters: SpecParametersMap[T]; type: T; }' is not assignable to type 'UserSpec | ClientSpec'.
    // Type '{ parameters: SpecParametersMap[T]; type: T; }' is not assignable to type 'ClientSpec'.
    // Types of property 'type' are incompatible.
    // Type 'T' is not assignable to type '"client"'.ts(2322)
    data: {
      parameters,
      type
    }
  }
}

doThing(SpecType.CLIENT, { foo: 4 })

游乐场link

【问题讨论】:

  • @captain-yossarian 看看这个
  • @jcalz 看看这个
  • @cyberwombat,你想创建一个生成器函数吗?我的意思是使用特定语法function*

标签: typescript


【解决方案1】:

这里的问题在于参数。 TS 不会将它们视为一个数据结构的一部分。

为了使它起作用,你应该将你的论点合并到一个数据结构中。

export enum SpecType {
  USER = 'user',
  CLIENT = 'client'
}

export interface Spec {
  type: `${SpecType}`
  parameters: unknown
}

export interface ClientSpecParameters {
  foo: number
}

export interface UserSpecParameters {
  bar?: number
}

export interface UserSpec extends Spec {
  type: `${SpecType.USER}`
  parameters: UserSpecParameters
}

export interface ClientSpec extends Spec {
  type: `${SpecType.CLIENT}`
  parameters: ClientSpecParameters
}

type AllowedValues = UserSpec | ClientSpec;

export interface AccountDefinition {
  [k: string]: AllowedValues
}


export const doThing = (data: AllowedValues): void => {
  const account: AccountDefinition = {
    foo: {
      type: 'client',
      parameters: {
        foo: 3
      }
    },
    data
  }
}

doThing({ type: SpecType.CLIENT, parameters: { foo: 4 } }) // ok
doThing({ type: SpecType.USER, parameters: { bar: 42 } }) // ok


您也可以使用this 方法,但您可能已经注意到,没有很好的解构

为什么不能使用泛型参数来获取两个参数?

您可以,实际上有几种方法可以做到这一点。这里有其中之一:

export const doThing = <T extends SpecType>(data: T extends SpecType.CLIENT ? ClientSpec : UserSpec): void => {
    const account: AccountDefinition = {
        foo: {
            type: 'client',
            parameters: {
                foo: 3
            }
        },
        data
    }
}

doThing({ type: SpecType.CLIENT, parameters: { foo: 4 } }) // ok
doThing({ type: SpecType.USER, parameters: { bar: 42 } }) // ok

doThing({ type: SpecType.USER, parameters: { foo: 4 } }) // expected error
doThing({ type: SpecType.CLIENT, parameters: { bar: 42 } }) // expected error

【讨论】:

  • 为什么不能使用泛型参数来获取两个参数?我以为会,但我的示例由于一个简单的错误而无法正常工作
  • @AmirSaleem 没问题!
  • 我很抱歉不清楚 - 我的意思是可以使用泛型来实现两个不同的函数参数,就像我在示例中使用的那样。使用泛型索引两个地图。例如doThing&lt;FOO&gt;(SpecType.CLIENT, { foo: 4 })
  • @cyberwombat 但是你在函数中的对象应该满足泛型的要求。请记住,TS 是关于静态类型的,而不是动态的。也许值得使用像这里stackoverflow.com/questions/68316470/…这样的咖喱函数?
猜你喜欢
  • 2014-03-09
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2013-04-03
  • 2016-05-13
相关资源
最近更新 更多