Skip to content

Commit ba0f5bb

Browse files
committed
update docusaurus
1 parent 1e40421 commit ba0f5bb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

website/src/pages/index.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ Template literal types are useful in various practical scenarios, such as:
345345
const customColor: Color = '#AD3128';
346346
```
347347

348+
- Database queries - Avoid using raw strings for table or column names, which can lead to typos and invalid queries. Use template literal types to define valid tables and column combinations.
349+
350+
<!-- prettier-ignore-start -->
351+
```ts
352+
// Avoid
353+
const query = 'SELECT name FROM usersss WHERE age > 30'; // Type 'string' - Typo 'usersss': table doesn't exist, leading to a runtime error.
354+
// Use
355+
type Table = 'users' | 'posts' | 'comments';
356+
type Column<TTableName extends Table> =
357+
TTableName extends 'users' ? 'id' | 'name' | 'age' :
358+
TTableName extends 'posts' ? 'id' | 'title' | 'content' :
359+
TTableName extends 'comments' ? 'id' | 'postId' | 'text' :
360+
never;
361+
362+
type Query<TTableName extends Table> = `SELECT ${Column<TTableName>} FROM ${TTableName} WHERE ${string}`;
363+
const userQuery: Query<'users'> = 'SELECT name FROM users WHERE age > 30'; // Valid query
364+
const invalidQuery: Query<'users'> = 'SELECT title FROM users WHERE age > 30'; // Error: 'title' is not a column in 'users' table.
365+
```
366+
<!-- prettier-ignore-end -->
367+
348368
### Type any & unknown
349369
350370
`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)