Skip to content

Commit a3ffe23

Browse files
committed
chore: copy to astro
1 parent 360f3ee commit a3ffe23

File tree

20 files changed

+578
-28
lines changed

20 files changed

+578
-28
lines changed

.vscode/ltex.dictionary.en-US.txt

+17
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@ eslintrc
2828

2929
npm
3030
pubDate
31+
Blackbook
32+
typescript-blackbook
33+
@astrojs
34+
sourcemap
35+
declarationMap
36+
tsc
37+
video-url
38+
jsonc
39+
camelCase
40+
PascalCase
41+
straw-hat-adr-file-convention
42+
ApiSpec
43+
APISpec
44+
apispec
45+
npmignore
46+
Deno
47+
JSDoc

apps/starlight/astro.config.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export default defineConfig({
1010
base: 'typescript-blackbook',
1111
integrations: [starlight({
1212
title: 'TypeScript Blackbook',
13+
favicon: './src/assets/logo.svg',
14+
logo: { src:'./src/assets/logo.svg'},
1315
social: {
1416
"x.com": 'https://x.com/unional',
1517
discord:'https://discord.gg/RwzcFpN5fv',
@@ -22,7 +24,7 @@ export default defineConfig({
2224
label: 'Guides',
2325
items: [
2426
// Each item here is one entry in the navigation menu.
25-
{ label: 'Example Guide', slug: 'guides/example' },
27+
{ slug: 'guides/welcome' },
2628
],
2729
},
2830
],

apps/starlight/src/content/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { z, defineCollection } from 'astro:content'
21
import { docsSchema } from '@astrojs/starlight/schema'
2+
import { defineCollection, z } from 'astro:content'
33

44
const blogsCollection = defineCollection({
55
type: 'content',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Creating rich comments
3+
tags: [comment, documentation, DX]
4+
---
5+
6+
You **should** add JSDoc comments to your code.
7+
8+
> Why?
9+
10+
For a long time, I do not do this.
11+
My belief was that the code should be self-explanatory.
12+
13+
However, having the comments in the code,
14+
especially for public facing code,
15+
makes it a lot easier for consumer to use the code.
16+
17+
Especially if you can provide examples in the comments.
18+
19+
For example, the following is a comment for the `IsEqual` type in [type-plus]:
20+
21+
```ts
22+
/**
23+
* Checks `A` and `B` are equal.
24+
*
25+
* ```ts
26+
* type R = IsEqual<1, 1> // true
27+
* type R = IsEqual<any, any> // true
28+
* type R = IsEqual<boolean, boolean> // true
29+
* type R = IsEqual<true, true> // true
30+
* type R = IsEqual<[1], [1]> // true
31+
*
32+
* type R = IsEqual<boolean, true> // false
33+
* type R = IsEqual<any, 1> // false
34+
* type R = IsEqual<[any], [1]> // false
35+
* type R = IsEqual<{ a: 1 }, { a: 1; b: 2 }> // false
36+
* ```
37+
*
38+
* Note that intersection type checks only works at first level.
39+
* It cannot be check recursively,
40+
* or else will run into infinite recursion if the type includes recursive types.
41+
*/
42+
export type IsEqual<A, B, Then = true, Else = false> = ...
43+
```
44+
45+
---
46+
47+
You **should not** include import statements in the comment examples.
48+
49+
> Why?
50+
51+
Your code or type can be reused in different context.
52+
The import statement might not be the same in different contexts.
53+
54+
For example, your code might be reused in another package.
55+
So the import statement will provide the wrong information.
56+
57+
❌ Bad
58+
59+
```ts
60+
/**
61+
* Check if the type `T` is exactly `any`.
62+
*
63+
* ```ts
64+
* import type { AnyType } from 'type-plus'
65+
*
66+
* type R = AnyType<any> // any
67+
*
68+
* type R = AnyType<never> // never
69+
* type R = AnyType<unknown> // never
70+
* type R = AnyType<string | boolean> // never
71+
* ```
72+
*/
73+
export type AnyType<T, Then = T, Else = never> = ...
74+
```
75+
76+
✅ Good
77+
78+
```ts
79+
/**
80+
* Check if the type `T` is exactly `any`.
81+
*
82+
* ```ts
83+
* type R = AnyType<any> // any
84+
*
85+
* type R = AnyType<never> // never
86+
* type R = AnyType<unknown> // never
87+
* type R = AnyType<string | boolean> // never
88+
* ```
89+
*/
90+
export type AnyType<T, Then = T, Else = never> = ...
91+
```
92+
93+
[type-plus]: https://github.com/unional/type-plus
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Naming Convention
3+
tags: [file, folder, project, naming]
4+
---
5+
6+
# Naming Convention
7+
8+
You **should** name your file and folder in `snake_case` or `kebab-case` instead of `camelCase` or `PascalCase`.
9+
10+
> Why?
11+
12+
Some file systems are case-insensitive (yes, I'm looking at you, Windows).
13+
That means `ApiSpec == apispec == APISpec`.
14+
15+
To avoid confusion, `camelCase` and `PascalCase` should be avoided.
16+
That leave us with `snake_case` or `kebab-case`.
17+
18+
The benefits of `snake_case` over `kebab-case` is that,
19+
in most cases, operating system treats `snake_case` as a single word,
20+
and treats `kebab-case` as a composed word.
21+
22+
For example, when you double-click on a `kebab-case` string,
23+
a single word will be selected (i.e. either `kebab` or `case` will be selected).
24+
25+
On the other hand, double-click on a `snake_case` string will select the whole string.
26+
27+
The same goes to renaming.
28+
29+
Therefore, I would recommend `snake_case` over `kebab-case`,
30+
even though `_` takes an additional pinky press to type.
31+
32+
But `kebab-case` is still a valid choice,
33+
especially when you are using file-based routing.
34+
35+
---
36+
37+
You **should** name your file in nouns. i.e. `customer_order.ts` instead of `create_customer_order.ts`.
38+
39+
> Why?
40+
41+
You might have heard of Single Responsibility Principle and think that you should have one function per file.
42+
43+
That is utterly wrong and not what SRP means.
44+
45+
SRP is about putting related code together where they have the same reason to change.
46+
47+
Naming (and thus organizing) the file with nouns will significantly make your file and folder a lot more stable.
48+
49+
Meaning there will be less import path changes.
50+
51+
---
52+
53+
You **can** use `.` to create sub-category on filenames.
54+
55+
> Why
56+
57+
The name of the file should describe WHAT the file is about,
58+
in terms of its context or business value.
59+
60+
For example, `customer_order.ts`.
61+
62+
However, there are situations that you want to further organize the code into addition categories,
63+
so that it is easy to visualize as well as controlling the exposed API.
64+
65+
For example, you can do this:
66+
67+
```sh
68+
customer_order.ts
69+
customer_order.ctx.ts # context code for dependency injection
70+
customer_order.internal.ts # code that you use internally, but exported for your tests.
71+
customer_order.mock.ts # helper code for mocking the data during tests
72+
customer_order.spec.ts # specification tests
73+
customer_order.unit.ts # unit tests
74+
customer_order.unit.electron.ts # unit tests only for electron
75+
```
76+
77+
## References
78+
79+
Another interesting take from [Straw Hat's ADRs][straw-hat-adr-file-convention].
80+
81+
[straw-hat-adr-file-convention]: https://straw-hat-team.github.io/adr/3122196229/README.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: ".npmignore file"
3+
authors: [unional]
4+
tags: [project]
5+
---
6+
7+
The `.npmignore` file is used to keep stuff out of your package.
8+
9+
You **should not** use `.npmignore` file
10+
11+
> Why?
12+
13+
There are [problematic situations](https://medium.com/@jdxcode/for-the-love-of-god-dont-use-npmignore-f93c08909d8d) when using `.npmignore` file.
14+
15+
You can actually exclude files and folders using the [files field](./package-json#the-files-field).
16+
There is no reason to use `.npmignore` file.
17+
18+
- <https://docs.npmjs.com/cli/v9/using-npm/developers#keeping-files-out-of-your-package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
slug: package-json
3+
title: "package.json"
4+
authors: [unional]
5+
tags: [project, typescript, tsconfig]
6+
---
7+
8+
## The `files` field
9+
10+
The `files` field is used to specify which files and folders to be included in the publish package.
11+
12+
You **should** always specify the `files` field.
13+
14+
> Why?
15+
16+
By default, files not excluded by `.gitignore` (or `.npmignore`) are included, which is not what you want.
17+
Also, files that are excluded by `.gitignore` are not included, which is likely also not what you want.
18+
19+
So it is always better to be explicit and control them yourself.
20+
21+
For example, if the `files` field is removed from [type-plus],
22+
23+
files like `.changeset/*`, `.github/*`, `.vscode/*` are included,
24+
while `cjs/*` and `esm/*` are not.
25+
26+
---
27+
28+
You **should** use `files` field to exclude test files.
29+
30+
For example, add this to your `files` field:
31+
32+
```json5
33+
{
34+
"files": [
35+
// your package files
36+
"cjs",
37+
"esm",
38+
"testing",
39+
"ts",
40+
// exclude test files
41+
"!**/*.{spec,test,unit,accept,integrate,system,perf,stress}.*"
42+
]
43+
}
44+
```
45+
46+
> Why?
47+
48+
Doing this allows you to keep your tsconfig setup simple.
49+
You will always compile all files, including your test files.
50+
51+
This ensures that your test files does not contain any syntax error.
52+
53+
- <https://docs.npmjs.com/cli/v9/configuring-npm/package-json#files>
54+
55+
[type-plus]: https://github.com/unional/type-plus
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
slug: package_name
3+
title: "Naming your package"
4+
authors: [unional]
5+
tags: [project]
6+
---
7+
8+
When you create a package,
9+
you always have to go through the painful process of naming your package.
10+
11+
While it is not specific to TypeScript,
12+
it is still beneficial to follow certain guidelines to make your life a bit easier.
13+
14+
---
15+
16+
You **should** use `snake_case` for your package name.
17+
18+
> Why?
19+
20+
[NodeJS] is not opinionated about package names, but
21+
[Deno] prohibits using `kebab-case` or `PascalCase` as module name.
22+
23+
Since [Deno] is likely to stay, you should use `snake_case` so that they are consistent.
24+
25+
---
26+
27+
You **should** name your package with nouns.
28+
29+
> Why?
30+
31+
Naming your package with verbs typically means your package is doing just one thing.
32+
33+
Of course, if that is what you want, that's fine.
34+
35+
But naming your package with nouns allows you to add similar features to your package, without causing confusion.
36+
37+
[Deno]: https://deno.land/x?page=2#Q&A
38+
[NodeJS]: https://nodejs.org/

apps/starlight/src/content/docs/guides/example.md

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Welcome
3+
description: Welcome old and new readers to TypeScript Blackbook.
4+
---
5+
6+
Welcome to TypeScript Blackbook.
7+
8+
This book focus on *how to get the most out of TypeScript with minimal effort*.
9+
10+
To achieve that goal,
11+
following some coding styles is a good place to start.
12+
13+
However, just following *what to do* can only go so far.
14+
You need to know *why* should you write code in certain way,
15+
*what* are the *trade-offs* you are making,
16+
as well as the design and the limitations of the language itself,
17+
so that you have the right *mindset* and *approach* the problem and come to a solution effectively.
18+
19+
Therefore, this book covers more than just style guide and best practices.
20+
21+
It needs to cover everything related to TypeScript in order to achieve that goal.
22+
23+
<!-- This book is organized into a few sections:
24+
25+
- How to TypeScript: Designs, limitations, and approach
26+
- TypeScript (and JavaScript) syntax and features
27+
- Coding Styles and Guidelines
28+
- Supporting Tools -->
29+
30+
Learning everything about TypeScript is not easy.
31+
32+
The language itself is pretty complex,
33+
and both TypeScript and JavaScript evolves at a rapid pace.
34+
So it can be quite overwhelming if you are just starting out.
35+
36+
I would recommend having a quick read through of the [How to TypeScript] section,
37+
and then check out the [Supporting Tools] section to find out how to set up your project,
38+
and use the rest of the book for reference as you need them.
39+
40+
Having that said,
41+
I'm in the process of updating this book.
42+
43+
Most of the information are still in their old format.
44+
So please head over to the [GitHub repo] to look for the original content for the time being.
45+
46+
[GitHub repo]: https://github.com/unional/typescript-blackbook
47+
[TypeScript Handbook]: https://www.typescriptlang.org/docs/handbook/intro.html

0 commit comments

Comments
 (0)