We use two objects to describe the salaries of two coders: const salary1 = { baseSalary: 100_000, yearlyBonus: 20_000 }; const salary2 = { contractSalary: 110_000 }; Then write a function to get the total salary function totalSalary(salaryObject: ???) { let total = 0; for (const name in salaryObject) { total += salaryObject[name]; } return total; } totalSalary(salary1); // => 120_000 totalSalary(salary2); // => 110_000 If it were you, how would you declare the The answer is to use an index signature! Next, let’s look at what 1. What is an index signature?The idea of index signatures is to type objects whose structure is unknown when only the key and value types are known. It fits perfectly in the case of We declare function totalSalary(salaryObject: { [key: string]: number }) { let total = 0; for (const name in salaryObject) { total += salaryObject[name]; } return total; } totalSalary(salary1); // => 120_000 totalSalary(salary2); // => 110_000
2. Index signature syntax The syntax for index signatures is fairly straightforward and looks similar to the syntax for properties, but there is one difference. We just write the key type inside square brackets instead of the property name: { [ Below are some examples of index signatures. interface StringByString { [key: string]: string; } const heroesInBooks: StringByString = { 'Gunslinger': 'Front-end Wisdom', 'Jack Torrance': 'Wang Dazhi' }; The interface Options { [key: string]: string | number | boolean; timeout: number; } const options: Options = { timeout: 1000, timeoutMessage: 'The request timed out!', isFileUpload: false }; The signature key can only be a 3. Notes on index signatures There are some caveats with index signatures in 3.1 Non-existent properties What happens if you try to access a non-existent property of an object with an index signature of { As expected, According to An index signature simply maps a key type to a value type, nothing more. If this mapping is not done correctly, the value type may deviate from the actual runtime data type. To make the input more accurate, the index value is marked as 3.2 string and number keysSuppose there is a dictionary of number names: interface NumbersNames { [key: string]: string } const names: NumbersNames = { '1': 'one', '2': 'two', '3': 'three', // ... }; No, it works normally. You can think of 4. Index signature vs. Record<Keys, Type> const object1: Record<string, string> = { prop: 'Value' }; // OK const object2: { [key: string]: string } = { prop: 'Value' }; // OK So the question is... when do you use As we know, index signatures only accept Index signatures are generic with respect to the keys. But we can use a union of string literals to describe the keys in type Salary = Record<'yearlySalary'|'yearlyBonus', number> const salary1: Salary = { 'yearlySalary': 120_000, 'yearlyBonus': 10_000 }; // OK
It is recommended to annotate generic objects with index signatures, e.g. the key is of type string. However, when you know the keys in advance, use Summarize: If you don't know the object structure you're dealing with, but you know the possible key and value types, then index signatures are what you need. An index signature consists of the index name and its type in square brackets, followed by a colon and the value type: This concludes this article on the understanding of TypeScript index signatures. For more related TypeScript index signature content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Solve the problem that Docker cannot ping the host machine under Mac
The <script> tag In HTML5, script has the f...
This article uses a specific example to introduce...
Table of contents Show Me The Code Test the effec...
Table of contents What is the Picker component Pr...
1. Unzip the zip package to the installation dire...
:is dynamic component Use v-bind:is="compone...
Table of contents The basic concept of modularity...
need: The official website's resource server ...
Docker private image library Docker private image...
1.MySQL UPDATE JOIN syntax In MySQL, you can use ...
MySQL partitioning is helpful for managing very l...
Some optimization rules for browser web pages Pag...
Separate the front and back ends and use nginx to...
Table of contents background Implementation ideas...
In the process of making web pages, we often use f...