最近发布了@types/node-observer包到npm,这里记录下发布过程
TS类型文件的包名通常以@types
开头,使用npm publish
发布以@types
开头的包时需要使用付费账号。

我们无需自己注册付费账号,可以将自己的代码merge
到开源项目DefinitelyTyped中,然后发布到npm上。
DefinitelyTyped
fork仓库DefinitelyTyped,拉取到本地(项目比较大,拉下来比较耗时)添加自己的代码、提交,然后创建Pull requests。勾选Pull requests
模板中的选项:

发起Pull requests
,会走一个CI流程,这个阶段耗费时间比较长,不知道是不是因为有墙的原因。期间出现几个错误记录记录如下:
index.d.ts头部解析错误
index.d.ts头部模板可参考:dt-header。这里出现两个错误:
Error: Could not parse version: line is '// TypeScript Version: 3.3.3' Error parsing header. Expected: foo MAJOR.MINOR (patch version not allowed). See: https://github.com/Microsoft/dtslint/blob/master/docs/dt-header.md
这两个错误属于同一类,注释中只能使用主版本号.次版本号
不能使用补丁版本号。修改后如下:

文件格式错误
.d.ts和.ts文件末尾要空一行

代码错误
export = observer;
等号两边要有空格:

const obj = new Demo();
这一行最初写的是let obj = new Demo();
,报ERROR: 9:5 prefer-const Identifier 'obj' is never reassigned; use 'const' instead of 'let'.
错误。
类定义Demo
的方法sayHello
不能使用public
修饰符,否则报ERROR: 4:5 member-access 'public' is implicit.
错误。

merge
,完成merge
后就会发布到npm了。
小结
以上是自己合并代码到主分支时遇到的错误,解决问题花费时间不算太长,倒是花费了大量时间等待CI结果。整体感觉,CI流程对文件内容的格式要求很严格。代码的提交、合并的详细流程可以点击此处查看:#40450,所有的构建历史记录可以在此处查看。
