Dictionary or map type in TypeScript
Dictionaries or maps are used to quickly retrieve items from an object. TypeScript does not have any concept of maps or dictionaries.
Plain JavaScript has objects that can set and retrieve key-value pairs. TypeScript provides Record
types that represent dictionaries or mappings through plain JavaScript objects.
Record
Types restrict keys and values to be set in plain JavaScript objects.
Record
Using types in TypeScript
Types in TypeScript Record
represent strict key-value pairs. More specifically, Record<K,V>
they mean that objects only accept types K
, and the values corresponding to those keys should be types V
.
Record<K,V>
A key of will yield K
as type, while is Record<K,V>[K]
equivalent to V
. Record
type is { [ key : K] : V }
an alias for index signatures such as .
The following code snippet shows the equivalent index signature type and Record
type structure used in TypeScript.
enum Level {
info = "info",
warning = "warning",
danger = "danger",
fatal = "fatal"
}
type LevelStrings = keyof typeof Level;
var bannerMessageInfo : Record<LevelStrings, string> = {
info : "[INFO]",
warning : "[WARNING]" ,
danger : "[DANGER]",
fatal : "[FATAL]"
};
function generateLogMessage(message : string , level : LevelStrings){
console.log(bannerMessageInfo[level] + " : " + message);
}
generateLogMessage("This is how Record Type can be used.", Level.info);
Output:
"[INFO] : This is how Record Type can be used."
The above Record
types can also be expressed as index signatures.
type BannerTypeMap = {
[level in LevelStrings] : string;
}
var bannerMessageInfo : BannerTypeMap = {
info : "[INFO]",
warning : "[WARNING]" ,
danger : "[DANGER]",
fatal : "[FATAL]"
};
BannerTypeMap
is a Mapped Type object in TypeScript, used here as an index signature and bannerMessageInfo
generates all type messages in .
In the example above, all fields in the map are required, otherwise TypeScript may not compile. Partial types can Record
be used with types to create stronger types.
Partial
Using and Record
types in TypeScript
Partial
The keyword can override some initial default properties with some passed properties. The following code snippet demonstrates this.
type PropStrings = 'height' | 'width' | 'shadow' ;
type Props = Record<PropStrings , number>
function CombineProps(props : Partial<Props> ){
var initProps : Props = {
height : 10,
width : 20,
shadow : 4
};
var finalProps = {...initProps , ...props};
console.log(finalProps);
}
CombineProps({width : 40});
Output:
{
"height": 10,
"width": 40,
"shadow": 4
}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Handling exceptions with try..catch..finally in TypeScript
Publish Date:2025/04/15 Views:170 Category:TypeScript
-
This article will discuss try..catch..finally handling exceptions in TypeScript using the statement. Handling exceptions in TypeScript In TypeScript, try..catch..finally a block handles exceptions that occur during program runtime. It allow
Convert JSON object to a class in TypeScript
Publish Date:2025/04/15 Views:80 Category:TypeScript
-
Data on the internet flows in the form of strings, which can be converted into a very popular format called JSON. The JSON representation of this data usually represents an object or even a class in TypeScript. TypeScript provides the abili
Update TypeScript to the latest version using NPM
Publish Date:2025/04/15 Views:119 Category:TypeScript
-
This article provides a guide to update to the latest version of TypeScript using npm, which is the best and most popular method used by developers. This also gives us an in-depth look at what npm is and why to use it. Node Package Manager
Using jQuery and TypeScript
Publish Date:2025/04/15 Views:148 Category:TypeScript
-
This article provides the basic understanding and concepts of using jQuery with TypeScript. It guides on how to use jQuery in TypeScript through a coding example and outputs using various methods in TypeScript and provides knowledge about w
Checking for undefined in TypeScript
Publish Date:2025/04/15 Views:196 Category:TypeScript
-
This article will demonstrate how programmers can check for undefined in TypeScript using various coding examples and situations. It not only gives you an understanding of checking for undefined in TypeScript, but also helps in differentiat
exclude attribute in TypeScript
Publish Date:2025/04/15 Views:190 Category:TypeScript
-
This article will discuss excluding properties from a TypeScript type. The exclude property is used to create another type from an existing type with fewer or modified properties. Omit Exclude properties from types in TypeScript TypeScript
Function return types in TypeScript
Publish Date:2025/04/15 Views:174 Category:TypeScript
-
TypeScript is a strongly typed language and it is always encouraged to define the types correctly for all variables used in TypeScript. These types can help in development later, especially during debugging. In addition to variables, functi
在 TypeScript 中使用 try..catch..finally 处理异常
Publish Date:2023/03/19 Views:396 Category:TypeScript
-
本文详细介绍了如何在 TypeScript 中使用 try..catch..finally 进行异常处理,并附有示例。
在 TypeScript 中使用 declare 关键字
Publish Date:2023/03/19 Views:257 Category:TypeScript
-
本教程指南通过特定的实现和编码示例深入了解了 TypeScript 中 declare 关键字的用途。