Skip to content

Commit 4d0ff16

Browse files
committed
update docusaurus
1 parent e220895 commit 4d0ff16

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

website/src/pages/index.mdx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -245,36 +245,6 @@ Consider benefits when explicitly typing the return value of a function:
245245
- Similar to writing tests before implementation (TDD), defining function arguments and return type, gives you the opportunity to discuss the feature functionality and its interface ahead of implementation.
246246
- Although type inference is very convenient, adding return types can save TypeScript compiler a lot of work.
247247

248-
### Template Literal Types
249-
250-
Embrace using template literal types, instead of just wide `string` type.
251-
Template literal types have many applicable use cases e.g. API endpoints, routing, internationalization, database queries, CSS typings ...
252-
253-
```ts
254-
// ❌ Avoid
255-
const userEndpoint = '/api/usersss'; // Type 'string' - Since typo 'usersss', route doesn't exist and results in runtime error
256-
// ✅ Use
257-
type ApiRoute = 'users' | 'posts' | 'comments';
258-
type ApiEndpoint = `/api/${ApiRoute}`; // Type ApiEndpoint = "/api/users" | "/api/posts" | "/api/comments"
259-
const userEndpoint: ApiEndpoint = '/api/users';
260-
261-
// ❌ Avoid
262-
const homeTitle = 'translation.homesss.title'; // Type 'string' - Since typo 'homesss', translation doesn't exist and results in runtime error
263-
// ✅ Use
264-
type LocaleKeyPages = 'home' | 'about' | 'contact';
265-
type TranslationKey = `translation.${LocaleKeyPages}.${string}`; // Type TranslationKey = `translation.home.${string}` | `translation.about.${string}` | `translation.contact.${string}`
266-
const homeTitle: TranslationKey = 'translation.home.title';
267-
268-
// ❌ Avoid
269-
const color = 'blue-450'; // Type 'string' - Since color 'blue-450' doesn't exist and results in runtime error
270-
// ✅ Use
271-
type BaseColor = 'blue' | 'red' | 'yellow' | 'gray';
272-
type Variant = 50 | 100 | 200 | 300 | 400;
273-
type Color = `${BaseColor}-${Variant}` | `#${string}`; // Type Color = "blue-50" | "blue-100" | "blue-200" ... | "red-50" | "red-100" ... | #${string}
274-
const iconColor: Color = 'blue-400';
275-
const customColor: Color = '#AD3128';
276-
```
277-
278248
### Type-Safe Constants with satisfies
279249

280250
The `as const satisfies` syntax is a powerful TypeScript feature that combines strict type-checking and immutability for constants. It is particularly useful when defining constants that need to conform to a specific type.
@@ -330,6 +300,36 @@ const IDLE_ORDER = {
330300
} as const satisfies OrderStatus;
331301
```
332302

303+
### Template Literal Types
304+
305+
Embrace using template literal types, instead of just wide `string` type.
306+
Template literal types have many applicable use cases e.g. API endpoints, routing, internationalization, database queries, CSS typings ...
307+
308+
```ts
309+
// ❌ Avoid
310+
const userEndpoint = '/api/usersss'; // Type 'string' - Since typo 'usersss', route doesn't exist and results in runtime error
311+
// ✅ Use
312+
type ApiRoute = 'users' | 'posts' | 'comments';
313+
type ApiEndpoint = `/api/${ApiRoute}`; // Type ApiEndpoint = "/api/users" | "/api/posts" | "/api/comments"
314+
const userEndpoint: ApiEndpoint = '/api/users';
315+
316+
// ❌ Avoid
317+
const homeTitle = 'translation.homesss.title'; // Type 'string' - Since typo 'homesss', translation doesn't exist and results in runtime error
318+
// ✅ Use
319+
type LocaleKeyPages = 'home' | 'about' | 'contact';
320+
type TranslationKey = `translation.${LocaleKeyPages}.${string}`; // Type TranslationKey = `translation.home.${string}` | `translation.about.${string}` | `translation.contact.${string}`
321+
const homeTitle: TranslationKey = 'translation.home.title';
322+
323+
// ❌ Avoid
324+
const color = 'blue-450'; // Type 'string' - Since color 'blue-450' doesn't exist and results in runtime error
325+
// ✅ Use
326+
type BaseColor = 'blue' | 'red' | 'yellow' | 'gray';
327+
type Variant = 50 | 100 | 200 | 300 | 400;
328+
type Color = `${BaseColor}-${Variant}` | `#${string}`; // Type Color = "blue-50" | "blue-100" | "blue-200" ... | "red-50" | "red-100" ... | #${string}
329+
const iconColor: Color = 'blue-400';
330+
const customColor: Color = '#AD3128';
331+
```
332+
333333
### Type any & unknown
334334

335335
`any` data type must not be used as it represents literally “any” value that TypeScript defaults to and skips type checking since it cannot infer the type. As such, `any` is dangerous, it can mask severe programming errors.

0 commit comments

Comments
 (0)