【问题标题】:multiple type with typescript带有打字稿的多种类型
【发布时间】:2022-11-10 18:53:30
【问题描述】:

我有我想用作类型的对象:

interface MyInput {
    id: string,
    label: string,
    setter: Function,
    type: string,
    value: string | string[] | boolean,
    options?: string[]
}

我有一些错误,例如:类型“字符串”上不存在属性“地图”|布尔值 |细绳[]'。 键入'字符串 |布尔值 | string[]' 不可分配给类型 'boolean |不明确的'。

问题是我将始终知道我的值是字符串 []、布尔值还是带有类型属性的字符串。

是否有任何解决方案可以在不创建 3 个不同界面的情况下消除这些错误?

除了将我的价值属性用于任何或创建 3 种不同类型之外,我不知道。

【问题讨论】:

  • The thing is that I will always know whether my value is string[], boolean or string with my type property.你能说的更具体点吗?你(人类)在寻找什么来判断它会是什么?

标签: javascript typescript next


【解决方案1】:

您需要做一些事情来投射它或使用@ts-ignore 对其进行注释。例如

(input.value as string[]).map(...

【讨论】:

    猜你喜欢
    • 2022-12-07
    • 2020-10-29
    • 2021-07-10
    • 2022-07-11
    • 2021-11-09
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多