TypeScript Parcel打包TS和全局类型
使用 Parcel 打包 TS 代码类似
webpack
- npm init =>package.json
- tsc --init =>tsconfig.json
- npm install parcel@next -D (比较粗略)
- tsconfig.json:“outDir”: “./dist” “rootDir”: “./src”
- package-lock.json:“test”: “parcel ./src/index.html”
- npm run test
全局类型
ts 引入 js包,类型没有定义,编辑器无法识别报红,需要安装.d.ts文件。
目前需求是自定义:jquery.d.ts
// index.html
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script src="./page.ts"></script>
需求一:
// page.ts
$(function () {
console.log(122);
});
// jquery.d.ts
// 方式一 定义全局变量
// declare声明var $
declare var $: (param: () => void) => void;
// 方式二 定义函数
// 定义全局函数
// 函数名为$
// 参数为:函数返回空的类型
// $函数返回为空
declare function $(param