TypeScript使用HashMap

I don't know how people use index types. I came across index types when I wanted to implement HashMap.
The index type looks like this:

{
    [idx: string|number]:any
}

Inside the square brackets is the type of the index, similar to the key in the hashmap, and supports two index signatures: string and number. Both types of indexes can be used at the same time, but the return value of a numeric index must be a subtype of the return value type of a string index. This is because when indexing with a number, JavaScript converts it to a string and then indexes the object. That is to say, indexing with 100 (a number) is equivalent to indexing with "100" (a string), so the two need to be consistent.

The type signature on the right is similar to the value in the hashmap.

E.g:

var userInfo: {[index:string]: string} = {}
userInfo["name"] = "typescript"
userInfo["age"] = "14"
console.log("user name is ", userInfo["name"])
console.log("user age is ", userInfo["age"])

For more content, welcome to join Chat: http://gitbook.cn/gitchat/activity/5a6043750b6ffc7ace5faf48

TypeScript Quick Start

How to use Python to crawl web pages to make ebooks

 

original address

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326414782&siteId=291194637