You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/src/pages/index.mdx
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,36 +245,6 @@ Consider benefits when explicitly typing the return value of a function:
245
245
- 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.
246
246
- Although type inference is very convenient, adding return types can save TypeScript compiler a lot of work.
247
247
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
-
typeApiRoute='users'|'posts'|'comments';
258
-
typeApiEndpoint=`/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
-
typeLocaleKeyPages='home'|'about'|'contact';
265
-
typeTranslationKey=`translation.${LocaleKeyPages}.${string}`; // Type TranslationKey = `translation.home.${string}` | `translation.about.${string}` | `translation.contact.${string}`
const color ='blue-450'; // Type 'string' - Since color 'blue-450' doesn't exist and results in runtime error
270
-
// ✅ Use
271
-
typeBaseColor='blue'|'red'|'yellow'|'gray';
272
-
typeVariant=50|100|200|300|400;
273
-
typeColor=`${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
-
278
248
### Type-Safe Constants with satisfies
279
249
280
250
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 = {
330
300
} asconstsatisfiesOrderStatus;
331
301
```
332
302
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
+
typeApiRoute='users'|'posts'|'comments';
313
+
typeApiEndpoint=`/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
+
typeLocaleKeyPages='home'|'about'|'contact';
320
+
typeTranslationKey=`translation.${LocaleKeyPages}.${string}`; // Type TranslationKey = `translation.home.${string}` | `translation.about.${string}` | `translation.contact.${string}`
const color ='blue-450'; // Type 'string' - Since color 'blue-450' doesn't exist and results in runtime error
325
+
// ✅ Use
326
+
typeBaseColor='blue'|'red'|'yellow'|'gray';
327
+
typeVariant=50|100|200|300|400;
328
+
typeColor=`${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
+
333
333
### Type any & unknown
334
334
335
335
`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