• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

编写TypeScript的库

武飞扬头像
niuge No.1
帮助1

准备技能:

1、准备package.json文件(用来发布npm包)

  1.  
    {
  2.  
    "name": "niuge-npm-test",
  3.  
    "version": "1.0.0",
  4.  
    "description": "My first npm test.",
  5.  
    "main": "index.js",
  6.  
    "type": "module",
  7.  
    "types": "index.d.ts",
  8.  
    "scripts": {
  9.  
    "test": "echo \"Error: no test specified\" && exit 1"
  10.  
    },
  11.  
    "repository": {
  12.  
    "type": "git",
  13.  
    "url": ""
  14.  
    },
  15.  
    "keywords": [
  16.  
    "npm",
  17.  
    "test"
  18.  
    ],
  19.  
    "author": "niuge8905",
  20.  
    "license": "ISC"
  21.  
    }
学新通

2、准备typescript的编译设置文件tsconfig.json

具体编译参数的说明:编译选项 · TypeScript中文网 · TypeScript——JavaScript的超集

  1.  
    {
  2.  
    "compilerOptions": {
  3.  
    "module": "ES6",
  4.  
    "noImplicitAny": true,
  5.  
    "removeComments": false,
  6.  
    "preserveConstEnums": true,
  7.  
    "outDir": "./lib",
  8.  
    "sourceMap": false,
  9.  
    "declaration": true
  10.  
    },
  11.  
    "include": [
  12.  
    "core/**/*"
  13.  
    ]
  14.  
    }

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文件。最终效果如下图:

学新通

样例代码位置:niuge-npm-test: niuge-npm-test

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghhge
系列文章
更多 icon
同类精品
更多 icon
继续加载