angular - 如何在 Typescript 中编译单个文件?

标签 angular typescript electron

因此,我正在尝试创建我的 Angular Web 应用程序的 Electron 版本,以在具有原生外观和感觉的桌面上运行。

我在 package.json 中有以下脚本

{
  "name": "app",
  "version": "1.0.0",
  "main": "main.js",
  "license": "MIT",
  "productName":"example",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build-prod": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron-tsc": "tsc main.ts && ng build --base-href ./ && electron .",
  },

main.ts 文件基本上就是这里给出的例子。 https://electronjs.org/docs/tutorial/first-app

问题是当我运行命令 'npm run electron-tsc' typescript 正在扫描我的目录的子文件夹并尝试编译我的整个 Angular 应用程序,但也有很多错误。

像这样。

node_modules/@types/selenium-webdriver/http.d.ts:24:14 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the lib compiler option to es2015 or later.

24 headers: Map; ~~~

node_modules/@types/selenium-webdriver/http.d.ts:48:14 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the lib compiler option to es2015 or later.

48 headers: Map; ~~~

node_modules/@types/selenium-webdriver/remote.d.ts:139:29 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the lib compiler option to es2015 or later.

139 setEnvironment(env: Map | {[name: string]: string} | null): this;



我的 main.ts 没有导入,命令指定只编译 main.ts
碰巧我仍然生成了一个有效的 main.js 文件,我可以运行 ng build --base-href ./ && electron .之后手动操作,一切正常,令人沮丧的是,我无法使用一个命令构建和运行它,而且我不理解错误。

这是我的 tsconfig.json
{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }

}

最佳答案

使用 --skipLibCheck 将抑制错误,而不是修复它们。短期内没关系(只是为了让你在处理根本原因时继续前进),但由于它完全退出库类型检查,所以它真的是一把枪。

要让 TypeScript 扫描声明文件并将其包含在编译中,它必须是您显式依赖的内容(您的项目中的 importrequire)或隐式依赖的内容(在您的 types 中添加到 tsconfig.json 中)。很多时候,它并不完全是你的依赖,而是你的依赖的依赖。为了真正的类型安全,树中的所有内容都必须有序。有时只是因为提供的类型不完善,需要修复。

您可以从更新 @types/selenium-webdriver 开始并更新您的依赖项。尝试运行 npx npm-check --update 以查看需要更新的内容。

如果这没有帮助,您可以为有问题的库编写类型声明(并将它们发送到DefinitelyTyped 或将它们保留在您的项目中)。

关于angular - 如何在 Typescript 中编译单个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53820228/

相关文章:

javascript - 获取所有系统字体 electron js

hyperlink - Electron - 链接在新窗口中打开

javascript - 类型错误 : json is not iterable

angular - 在 Angular 中加载 App 组件以外的另一个组件

Typescript 构建从 node_modules 文件夹中获取错误

angular - 为什么 HttpEventType.UploadProgress 事件在 Angular 文件上传请求中只执行一次?

html - 如何在没有第三方的情况下使用 Angular/Typescript 对 HTML 表格进行排序?

javascript - 如何在 React 中使网站主题在刷新时保持不变?

angular - 如何在 ngFor 循环中创建变量?

angular - 使用 Angular Material 创建响应式工具栏