简体   繁体   中英

Map data structure in strict mode

I am using strict mode for the following code. Trying to assign an object value from a Map. It is granted that the value I am getting from the Map isn't undefined but strict mode giving the following error Type A[]|undefined isn't assignable to type A[] . How can I pass this compiler issue?

let key = "foo"
leaderboardRecords: A[] = [];
leaderboardRecordsbyRegion: Map<string, A[]> = new Map<string, A[]>();
if (!leaderboardRecordsbyRegion.has(key)) {
        leaderboardRecordsbyRegion.set(key, fetchData());
    }
leaderboardRecords = leaderboardRecordsbyRegion.get(key);

This is how I solved it in the end. I added or empty array to pass the compiler.

leaderboardRecords = leaderboardRecordsbyRegion.get(key) || [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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