【问题标题】:Nested Typescript Map Value Type嵌套 Typescript 映射值类型
【发布时间】:2020-11-24 04:29:13
【问题描述】:

类似于Nested Typescript Map Type,但嵌套在“值”端。

Typescript Playground

const mapObjectObject: Map<string, string | Map<string, string>> = new Map(Object.entries({
    "a": "b",
    "c": new Map(Object.entries({
        "d": "e",
    }))
})); // ✓

const mapObjectMap: Map<string, string | Map<string, string>> = new Map(Object.entries({
    "a": "b",
    "c": new Map([
        ["d", "e"]
    ])
})); // ✓

const mapMapObject: Map<string, string | Map<string, string>> = new Map([
    ["a", "b"],
    ["c", new Map(Object.entries({
        "d": "e",
    }))
]); // ✗


const mapMapMap: Map<string, string | Map<string, string>> = new Map([
    ["a", "b"],
    ["c", new Map([
        ["d", "e"]
    ])]
]); // ✗

为什么前两个有效,而后两个无效。他们都错误:

  Overload 1 of 3, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
    Argument of type '([string, string] | [string, Map<string, string>])[]' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
      The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
        Type 'IteratorResult<[string, string] | [string, Map<string, string>], any>' is not assignable to type 'IteratorResult<readonly [string, string], any>'.
          Type 'IteratorYieldResult<[string, string] | [string, Map<string, string>]>' is not assignable to type 'IteratorResult<readonly [string, string], any>'.
            Type 'IteratorYieldResult<[string, string] | [string, Map<string, string>]>' is not assignable to type 'IteratorYieldResult<readonly [string, string]>'.
              Type '[string, string] | [string, Map<string, string>]' is not assignable to type 'readonly [string, string]'.
                Type '[string, Map<string, string>]' is not assignable to type 'readonly [string, string]'.
                  Types of property '1' are incompatible.
                    Type 'Map<string, string>' is not assignable to type 'string'.
  Overload 2 of 3, '(entries?: readonly (readonly [string, string])[] | null | undefined): Map<string, string>', gave the following error.
    Type 'Map<string, string>' is not assignable to type 'string'.(2769)

No overload matches this call.
  Overload 1 of 3, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
    Argument of type '([string, string] | [string, Map<string, string>])[]' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
  Overload 2 of 3, '(entries?: readonly (readonly [string, string])[] | null | undefined): Map<string, string>', gave the following error.
    Type 'Map<string, string>' is not assignable to type 'string'.(2769)

分别。删除类型声明也无济于事。推断类型也有同样的错误。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    你应该给 TS 一个关于类型的提示:

    const mapMapObject = new Map<string, string | Map<string, string>>([
        ["a", "b"],
        ["c", new Map(Object.entries({
            "d": "e",
        }))
    ]]);
    
    const mapMapMap = new Map<string, string | Map<string, string>>([
        ["a", "b"],
        ["c", new Map([
            ["d", "e"]
        ])]
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 2017-08-08
      • 2020-08-29
      • 2013-10-19
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多