最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Map of Maps in TypescriptECMAScript - Stack Overflow

programmeradmin2浏览0评论

This is what i personally would consider correct in various languages, but here it is not. I must be ignorant of an aspect of Typescript, but I can not so easy determine where the gap is.

The implementation requires a map of maps. The top map is a string key, and the a map of string-keys and string-values.

class aClass
{
    myMap: Map<string, Map<string, string>>;

    constructor ()
    {
        this.myMap = new Map<string, Map<string, string>>([
            ["key1", new Map<string,string>()],
            ["key2", new Map<string,string>()]
        ]);
    }

    async aTask(map: Map<string,string>)
    {
        map.set("subKey1", "VALUE");
    }

    async someWork()
    {
        this.aTask(this.myMap["key1"]);
        this.aTask(this.myMap.get("key1"));
    }
}

How do I correctly access the Map<string, string>() in the function someWork() ?

The error for the first accessor : this.aTask(this.myMap["key1"]);

Element implicitly has an 'any' type because type 'Map<string, Map<string, string>>' has no index signature. Did you mean to call 'get'?

The error for the second accessor : this.aTask(this.myMap.get("key1"));

Argument of type 'Map<string, string> | undefined' is not assignable to parameter of type 'Map<string, string>'. Type 'undefined' is not assignable to type 'Map<string, string>'.

This is what i personally would consider correct in various languages, but here it is not. I must be ignorant of an aspect of Typescript, but I can not so easy determine where the gap is.

The implementation requires a map of maps. The top map is a string key, and the a map of string-keys and string-values.

class aClass
{
    myMap: Map<string, Map<string, string>>;

    constructor ()
    {
        this.myMap = new Map<string, Map<string, string>>([
            ["key1", new Map<string,string>()],
            ["key2", new Map<string,string>()]
        ]);
    }

    async aTask(map: Map<string,string>)
    {
        map.set("subKey1", "VALUE");
    }

    async someWork()
    {
        this.aTask(this.myMap["key1"]);
        this.aTask(this.myMap.get("key1"));
    }
}

How do I correctly access the Map<string, string>() in the function someWork() ?

The error for the first accessor : this.aTask(this.myMap["key1"]);

Element implicitly has an 'any' type because type 'Map<string, Map<string, string>>' has no index signature. Did you mean to call 'get'?

The error for the second accessor : this.aTask(this.myMap.get("key1"));

Argument of type 'Map<string, string> | undefined' is not assignable to parameter of type 'Map<string, string>'. Type 'undefined' is not assignable to type 'Map<string, string>'.

Share Improve this question edited Oct 20, 2020 at 0:53 asked Oct 20, 2020 at 0:48 user10717747user10717747 4
  • When I have a question about JavaScript, my first stop is MDN, which has great docs on Map. – Heretic Monkey Commented Oct 20, 2020 at 0:52
  • @HereticMonkey I do indeed read those docs, but this it turns out is a bit of EBNF that needs unpicking to make sense of the error. – user10717747 Commented Oct 20, 2020 at 1:01
  • To be fair, there was no TypeScript error in the question when I made that ment. – Heretic Monkey Commented Oct 20, 2020 at 1:05
  • Have you tried to do what the error message tells you to do? – Jörg W Mittag Commented Oct 20, 2020 at 6:14
Add a ment  | 

1 Answer 1

Reset to default 2

Maps, despite being objects, don't put their values onto properties of the Map instance, which is why this.myMap["key1"] doesn't work.

TypeScript doesn't let you do

this.aTask(this.myMap.get("key1"));

because the .get does not guarantee that the key exists in the Map; it's warning you that it might have returned undefined. To be type-safe, do:

async someWork() {
    const val = this.myMap.get("key1");
    if (val) {
        this.aTask(val);
    }
}
发布评论

评论列表(0)

  1. 暂无评论