Typescript中是否提供键值对?

Cod*_*ter 68 typescript

打字稿中是否有键值对?如果是的话怎么做.任何人都可以提供示例示例链接

bas*_*rat 92

Typescript中是否提供键值对?

是.称为索引签名:

interface Foo {
   [key: string]: Bar;
} 
let foo:Foo = {};
Run Code Online (Sandbox Code Playgroud)

string和值是Bar.

更多

您可以使用ES6 Map适当的字典,通过polyfilledcore-js.

  • 这是地图或字典。如果您想要与 C# KeyValuePair 等效的东西,请参阅答案 /sf/ask/2552722861/#50621451 (2认同)
  • 也很高兴知道,您无需将其称为“键”。例如,您也可以这样写:`[countryCode:string]:string`。可读性好。 (2认同)
  • 这是更新后的链接:https://basarat.gitbook.io/typescript/type-system/index-signatures (2认同)
  • 是的,这是一张地图。但每次我搜索“TypeScript Map”时,我得到的都是 es6 Map,这不是我想要的!所以我非常感谢这个答案在这里。 (2认同)

Jai*_*ime 51

最简单的方法是:

var indexedArray: {[key: string]: number}
Run Code Online (Sandbox Code Playgroud)

  • 这很有帮助-比接口方法少的代码,因为它不需要描述`Bar` (2认同)
  • 索引数组 = {}; 索引数组[“一”] = 1; /* 好 **/ indexedArray["two"] = "two"; /* 失败 */ (2认同)

mag*_*ker 25

你也可以考虑使用Record,像这样:

const someArray: Record<string, string>[] = [
    {'first': 'one'},
    {'second': 'two'}
];
Run Code Online (Sandbox Code Playgroud)

或者这样写:

const someArray: {key: string, value: string}[] = [
    {key: 'first', value: 'one'},
    {key: 'second', value: 'two'}
];
Run Code Online (Sandbox Code Playgroud)


Jac*_*ler 14

Typescript中是否可以使用键值对?

如果您想到的是C#KeyValuePair <string,string>:不,但是您可以轻松地自己定义一个:

interface KeyValuePair {
    key: string;
    value: string;
}
Run Code Online (Sandbox Code Playgroud)

用法:

let foo: KeyValuePair = { key: "k", value: "val" };
Run Code Online (Sandbox Code Playgroud)

  • 我也想知道。失望的选民永远不会发表评论,我有时认为是因为他们自己不知道任何更好的解决方案。 (6认同)
  • 有点滑稽,已经有6票,总和为0。拒绝投票的人会介意添加评论对此解决方案有何不利之处? (4认同)
  • **“KeyValuePair”不是列表**。其中只有一个键和一个值。键:`foo.key` 和值:`foo.value`。看来您正在寻找 `List&lt;&gt;` --&gt; /sf/ask/1616738231/ (4认同)
  • @DaNeSh是的。“ KeyValuePair”不是列表,但可以是列表的条目。也许您正在寻找`List &lt;&gt;`?参见/sf/ask/1616738231/ (2认同)

for*_*d04 12

一种简洁的方法是使用元组作为键值对

const keyVal: [string, string] =  ["key", "value"] // explicit type
const keyVal2 = ["key", "value"] as const // inferred type with const assertion
const [key, val] = ["key", "val"] // usage with array destructuring
Run Code Online (Sandbox Code Playgroud)

您可以创建一个泛型KeyValuePair类型以实现可重用性:

type KeyValuePair<K extends PropertyKey, V = unknown> = [K, V]
const kv: KeyValuePair<string, string> = ["key", "value"]
Run Code Online (Sandbox Code Playgroud)

TS 4.0

提供 带标签的元组元素以获得更好的文档和工具支持:

type KeyValuePairNamed = [key: string, value: string] // "key" and "value" labels
Run Code Online (Sandbox Code Playgroud)

兼容性

[key, value]元组还确保与 JS 内置对象的兼容性:

操场


pet*_*r70 10

不是提问者,而是所有其他感兴趣的人:请参阅:如何定义键值对的Typescript Map.其中key是数字,value是对象数组

因此解决方案是:

let yourVar: Map<YourKeyType, YourValueType>;
// now you can use it:
yourVar = new Map<YourKeyType, YourValueType>();
yourVar[YourKeyType] = <YourValueType> yourValue;
Run Code Online (Sandbox Code Playgroud)

干杯!


Jac*_*ler 9

另一种简单的方法是使用元组

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10];
// Access elements
console.log("First: " + x["0"] + " Second: " + x["1"]);
Run Code Online (Sandbox Code Playgroud)

输出:

第一:你好第二:10


小智 9

键值对的示例是:

[key: string]: string

您可以输入任何内容作为值-如果您以编程方式填充了类型不同的值,则可以输入:

[key: string]: any

尽管您可以自行决定any:)


小智 5

也可以简单的使用Record

type Foo = Record<string, number>
Run Code Online (Sandbox Code Playgroud)

文档中的进一步用法