准备技能:
- 熟悉TypeScript的知识:文档简介 · TypeScript中文网 · TypeScript——JavaScript的超集
- 熟悉npm发布库的方法:Getting started | npm Docs
1、准备package.json文件(用来发布npm包)
{
"name": "niuge-npm-test",
"version": "1.0.0",
"description": "My first npm test.",
"main": "index.js",
"type": "module",
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": ""
},
"keywords": [
"npm",
"test"
],
"author": "niuge8905",
"license": "ISC"
}
2、准备typescript的编译设置文件tsconfig.json
具体编译参数的说明:编译选项 · TypeScript中文网 · TypeScript——JavaScript的超集
{
"compilerOptions": {
"module": "ES6",
"noImplicitAny": true,
"removeComments": false,
"preserveConstEnums": true,
"outDir": "./lib",
"sourceMap": false,
"declaration": true
},
"include": [
"core/**/*"
]
}
3、创建core、lib文件夹,创建index.js文件和index.d.ts文件
其中就如第2步配置的一样,core是ts的源文件位置,lib是编译后文件存放的位置。
core文件夹里创建几个测试的ts文件。
index.js和index.d.ts引入core里的文件
4、命令行输入tsc
最后,lib文件夹里自动生成js文件和.d.ts文件。最终效果如下图: