Skip to content

Commit 234a70f

Browse files
authored
docs(blog): update for snippets (#6635)
1 parent 017e68f commit 234a70f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

documentation/blog/2023-06-16-typescript-record.md renamed to documentation/blog/2025-01-06-typescript-record.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-06-16-typescript
88
hide_table_of_contents: false
99
---
1010

11+
**This article was last updated on January 6, 2025, to include sections on Common Mistakes with TypeScript Record and Tips for Using Record in TypeScript.**
12+
13+
## What is Record Type in TypeScript?
14+
1115
The `Record<>` utility type in TypeScript is typically associated with a record or a collection of records returned from an API endpoint. It helps define a type with property names such as `id` and map the values to the type of the data.
1216

1317
## Introduction
@@ -16,6 +20,33 @@ The `Record<>` type is a TypeScript object transformation type that is often use
1620

1721
This post explores the TypeScript Record type with a series of examples ranging from simple strings and numbers based types to more common ones involving API data and React components. With the examples, we see how to derive `Record<>` types by assigning types for `Keys` and `Value`, discuss how a `Record<>` type is advantageous over simple object types and index signatures, and try to understand the quirks of its usage.
1822

23+
:::tip FAQs about TypeScript Record Type
24+
25+
- Q: What is the `Record<>` type in TypeScript?
26+
The `Record<>` type is useful in specifying an object whose keys and values have explicit types. Ensure type safety for dynamic objects.
27+
28+
- Can the `Record<>` type have keys other than `string`?
29+
Yes, for the keys we can use `string`, `number`, or `symbol`. All other types are prohibited for the keys.
30+
31+
- How does `Record<>` differ from the index signature?
32+
`Record<>` provides stricter type checking, while index signatures (`[key: string]: Value`) are more flexible but less type-safe.
33+
34+
- Can I use the `Record<>` type with React components?
35+
Yes, `Record<>` does work with JSX components as values. You can map component names or props to components.
36+
37+
- How do I constrain keys in a `Record<>` type?
38+
39+
You can define a union of allowed keys for the `Keys` type.
40+
41+
For example:
42+
43+
```ts
44+
type Permissions = "Admin" | "User" | "Guest";
45+
type PermissionMap = Record<Permissions, string>;
46+
```
47+
48+
:::
49+
1950
## Understanding the Record Type
2051

2152
Starting easy, let's begin with a simple **object type** that represents a user:
@@ -257,6 +288,37 @@ const dashboardPreviews: TDashboardPreview = {
257288

258289
We can then use the map inside main dash page.
259290

291+
## Common Mistakes and Best Practices
292+
293+
### Using Types Not Allowed for Keys
294+
295+
- Keys must be of type `string`, `number`, or `symbol`. Types like `boolean` are not permitted.
296+
297+
```ts
298+
type InvalidRecord = Record<boolean, string>; // error
299+
```
300+
301+
### Confusing Keys and Values
302+
303+
- It is easy for developers to think that `Record<>` enforces key and value constraints together, which is just not true. Remember `Keys` applies only to the names of the properties.
304+
305+
```typescript
306+
type Example = Record<string, number>;
307+
const data: Example = { key: "value" }; // Error: "value" is not a number
308+
```
309+
310+
### Over-Complicating the Type
311+
312+
- If it's something you can dynamically generate with enums, or mapped types then don't manually define every key in a union.
313+
314+
### Best Practices
315+
316+
- Use `Record<>` when: - You need type-safe objects with dynamic keys.
317+
- You must map keys to complex types, like objects, components or unions.
318+
- You need a lightweight alternative to creating interfaces or types for objects with simple mappings.
319+
- Use unions to constrain keys instead of an unnecessary flexibility being given.
320+
- Validate API responses when using `Record<>` to map backend data.
321+
260322
## Summary
261323

262324
In this post we explored how to use the `Record<>` type in TypeScript to construct stable types that are error-prone and more maintainable. We saw how the derived type is a hash map based on a type that represents the actual shape of the data. It also accepts and assigns types to member keys of the map, which can be restricted by using a union type. We have seen an example of using `Record<>` to type `users` data for an API endpoint as well as one example that uses `Record<>` type for rendering React components.

0 commit comments

Comments
 (0)