TypeScript 引入js库

TypeScript 引入js库

如果要使用现有的js文件的话,需要对编译器增加–allowJS参数。可以修改tsconfig.json中的compilerOptions,将其中的"allowJs"设为true,如: “allowJs”: true,没有该字段的话增加该字段即可。另外在tsconfig中的"include"字段中将js文件的路径添加进去。建议增加一个专门的文件夹,用来存放所有要引入的js文件。比如都放到一个名为"jslibs"的目录中,然后在tsconfig.json的"include"字段中增加配置,匹配"jslibs"下的所有js文件,如:

{
  "compilerOptions": {
    "strictNullChecks": true,
    "noImplicitAny": true,
    "module": "CommonJS",
    "target": "ES5",
    "allowJs": true,
    "experimentalDecorators": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "inlineSourceMap": true,
    "inlineSources": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strict": true,
    "removeComments": true,
    "pretty": true,
    "strictPropertyInitialization": true,
    "typeRoots": [
      "typings"
    ]
  },
  "include": [
    "./**/*.ts",
    "./src/jslibs/*.js"
  ],
  "exclude": [
    "node_modules",
    "miniprogram_dist",
    "**/*.spec.ts"
  ]
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值