This commit is contained in:
2026-11-29 07:30:49 +08:00
parent ea56e4cabd
commit 0026089761
71 changed files with 8054 additions and 1428 deletions
+124
View File
@@ -0,0 +1,124 @@
## 不采用任何语言的 enum 特性,彻底抛弃这一语言特性
本来使用 enum 就是为了限制值的选项,结果两个 enum 不存在任何子集关系。
```
enum Size {
XS = "xs",
}
enum ButtonSize {
XS = "xs",
}
assert.equal(Size.XS != ButtonSize.XS);
```
## 不采用 c++ 和 rust 语言的 union 特性,彻底抛弃这一语言特性
```
union Size {
xs:string,
// Could't write the fix value field.
// example: xs:string = "xs"
}
```
## 采用 ts 语言的 union type 特性,union 就应该是一个简单的,不统一类型的集合
```
type Size = "xs"| "sm" | undefind;
```
编译时可以确定大小,只需要取最大的那个即可。
可见这个类型会自动解值,
## 采用 rust 的借用检查,所有权,生命周期这些特性
## ts 的可选参数特性应该采用
```
type Size = "xs" | "md";
function size_recipe(size?:Size){}
// 注意,形参后面加?, 才可以省略,而不是下面这样。
type Size2 = "xs" | "md" | undefind;
// 这样是不可以省略的,undefined 只是其中一个选项而已。
function size_recipe(size:Size2){}
```
## 不采用 ts 对象特性
ts 的对象,必须要使用字段名访问,例如 obj.name ,且字段没有任何类型可言。
同名字段,不同类型,后面的可以覆盖前面的,最后导致类型变更,这导致编译时期无法确定大小。
我认为,每个字段都必须都有类型,字段可以是常量,string 类型常量, number 类型常量,他们的值是固定的。
这其实就是所谓的 class ,内部有一堆 static 常量,还有一些带类型的属性变量。
## 不允许 ts 的匿名类型
实战中,经常需要将多个函数的返回值,整合成一个类型。
ReturnType 是编译时动态推断的,改函数内的代码,不用修改返回的类型,非常好用。
但我希望强制起名,在函数圆括号后面,直接起一个名就好。
如果类型起了名字,那么直接用 {} & {} 字段扁平化 ,或者 A {a:B,c:C} 组成一个新的类型。
有些方法的泛型要传入一个类型,如果是匿名类型,且有一大堆函数,我们不可能一个个给它写完。
## 采用,ts 中的类型,其实都是一个类型集合
我发现,除了 TS ,其他语言的类型都是固定的。
TS 中的所有类型,其实都是 union type,我称呼这个为 “ union everywhere ”。
## 采用,模块使用双冒号::
成员变量访问使用点.语法。
类型声明使用单冒号: 。
数组成员访问,使用[key],或者[0] 。
这样的好处是,减少 AST 语义分析的难度。
试想一下,你发现一个点语法,难道你还要全文检索,这个到底是成员变量,还是子模块吗?
## 起名规则,采用 snake 命名更好,兼顾阅读美观,和避免与运算符混淆
snake 模式,极其容易看漏空格。
camel 模式,全大写的专业术语,会跟后一个单词拼接在一起。
短横线模式,容易误认为是运算符。
## ts 配置文件中的 path 是外部模块,算当前工程的一部分,而不是 devDep
path 是关于,什么属于当前源码的一部分,无关 dep 和 devDep。
但如果,你希望使用另一个项目作为 devDep,当作插件临时使用,那就不要写 path。
## 我制定的打包器项目结构
dist,作为编译输出目录 等价于。
index.js,作为入口文件名,不同语法的,会插入第二扩展名,例如,index.es.js。
这主要是防止增加文件复杂度,umd 并不属于 es 和 cjs 分类,没必要弄出一种 ujs 的新扩展名。
要是出现更多的情况要怎么办?给扫描器增加一大堆负担,babel 懂 ujs,cjs,mjs 这些格式吗。
你说防止语法检查器误改?确实 eslint 可能会破坏 umd 的书写语法。
声明文件,我自己创作的语言是强类型的,不需要这个声明文件。
但如如果是给 ts 搞,那就固定为 index.d.ts 。
+30
View File
@@ -0,0 +1,30 @@
## type
需要填 “module”,否则就是 cjs 。
## 永远不要使用 EsNext,它不断变化,除非你想体验新功能
不断变化,意味着,一个语法今天可用,明天不可用,并不是我们想要的只写一次,永远不用改。
你始终应该使用最后一个版本,或者固定一个版本。
## moduleResolution 定义接受的语法
在 es 标准中, 就是一个文件,就默认是一个 module,Resolution 就是如何解析读取文件。
这个就是 ts 编译器定义 “有效文件” 的,使用外部打包器,例如 Vite,就填 bundler,否则填 nodenext。
nodenext 就是接受一切语法的意思。
最后提醒一下,其他语言并不是都和 es一样,把每个文件都看作一个 module。
## module 定义本项目产物的输出语法
通常应该使用最后一个 esxxxx 版本,如果要为老项目增加新功能,就不要超过老项目的版本。
例如,一个老项目的 module 是 es2022,你写了一个插件,那么你插件的 module 不得超过es2022 。
## monorepo 目前只有一种方案, 就是 pnpm
试过了很多 npm, yarn, turborepo, bun, 都是不行 。
+69
View File
@@ -0,0 +1,69 @@
{
// 记住,【ts 库】,【ts + react 库】,【ts + react 应用】,是三个不同的配置。
// 其中【ts + react 应用】是一定要用 bundler 的,因为树摇很重要。
// 剩下两个,一般也有很多 ts 以外的文件,例如一大堆 css,svg,png文件。
"compilerOptions": {
// Option 1: If use Vite, set "bundler", let Vite to decision.
// Option 2: set "nodenext" to search and read all kinds of grammar file.
"moduleResolution": "nodenext",
// The write grammar for current project.
// "esxxxx" only can use with "moduleResolution":"bundler".
// this value must lower than moduleResolution, let current code can be resolution.
"module": "nodenext",
// The final complied output file grammar.
// Not influence the grammar you write code.
// Usually lower than newst version 4 year version for compatiblility.
"target": "es2022",
// If your code will run in browser, maybe you need this external API.
// If your code is pure backend, and not web server like Nextjs, you can delete this line.
// But node-web stack tech is all for web, you just set [ target, dom, dom.iterable ] enough.
"lib": ["es2022", "DOM", "DOM.Iterable"],
// Option 1: "rootDir":"." and "exclude":[] long list.
// Ts grammar will check vite.config.ts file outside the src folder.
// Option 2: "rootDir":"src"
// Ts grammar will ignore check vite.config.ts file that outside the src folder
// You will get error.
// I recommend use Option 1.
"rootDir": ".",
// If not use Vite, you use tsc, must specify this field.
"outDir": "./dist",
// package.json won't auto put the @types/node declare file in global scope.
// Ts grammar check will get error, "vite/client" is same.
"types": ["node", "vite/client"],
// Do not emit compiled output files, vite will take care of the that.
// if you not use vite, dist foloder is nothing.
"noEmit": true
// Vite will handle the resolution of the import paths.
},
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"],
"exclude": [
"node_modules",
"dist",
".turbo/**/*",
".cache/**/*",
".vite/**/*",
"vite.config.ts",
"*.config.ts",
"*.config.js",
"tsconfig.*.json",
"tsconfig.*",
"__tests__/**/*",
"test/**/*",
"tests/**/*",
"**/*.test.ts",
"**/*.test.tsx",
"**/*.spec.ts",
"**/*.spec.tsx",
".storybook/**/*",
"stories/**/*",
"example/**/*",
"examples/**/*",
"scripts/**/*",
".env",
".env.*",
"public/**/*",
"docs/**/*",
"README.md",
"LICENSE"
]
}