Typescript: What exactly refers the type "{[name:string]: string}" to?

StackOverflow https://stackoverflow.com/questions/23675507

  •  23-07-2023
  •  | 
  •  

Вопрос

I looked into some TypeScript source code lately and came across a property typed like this:

private _properties: {[name:string]: string};

I asked myself what this type refers to? What is it exactly?

I thought it would be an object where key and value need to be string.

But I tried it in the TypeScript Playground and saw that something like

this._properties['name'] = 5;

is also valid. So what's the complete explanation of this type?

Это было полезно?

Решение

No you're right the first time. _properties is a map of string to string.

Here's an example on the TypeScript Playground demonstrating the expected error.

If you can link an example where that statement is valid we can figure out why.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top