5

关于 Angular 应用 tsconfig.json 中的 lib 属性

 3 years ago
source link: https://segmentfault.com/a/1190000041142580
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

关于 Angular 应用 tsconfig.json 中的 lib 属性

发布于 今天 03:53

SAP Spartacus 应用的 tsconfig.json 文件里,有一个 lib 属性,值为 es2020 和 dom:

TypeScript 包括一组内置 JS API(如 Math)的默认类型定义,以及浏览器环境(如 document)中的类型定义。 TypeScript 还包括用于与您指定的目标匹配的较新 JS 功能的 API; 例如,如果目标是 ES6 或更高版本,则 Map 的定义可用。

我们可以根据实际需要来修改 lib 属性的值。以下是一些常见场景:

  • 您的程序不在浏览器中运行,因此您不需要“dom”类型定义
  • 您的运行时平台提供了某些 JavaScript API 对象(可能通过 polyfills),但尚不支持给定 ECMAScript 版本的完整语法
  • 您有一些(但不是全部)更高级别 ECMAScript 版本的 polyfill 或本机实现

lib 支持下列值:

  • ES5: 所有 ES3 和 ES5 功能的 ES5 核心定义
  • ES2015:ES2015(也称为 ES6)中可用的其他 API,比如 array.find、Promise、Proxy、Symbol、Map、Set、Reflect 等。
  • ES6:“ES2015”的别名
  • ES2016 ES2016 中可用的其他 API,比如 array.include 等。
  • ES7: “ES2016”的别名
  • ES2017: ES2017 中可用的其他 API, 比如 Object.entries、Object.values、Atomics、SharedArrayBuffer、date.formatToParts、类型化数组等。
  • ES2018:ES2018 中可用的其他 API - async iterables、promise.finally、Intl.PluralRules、regexp.groups 等。
  • ES2019:ES2019 中可用的其他 API - array.flat、array.flatMap、Object.fromEntries、string.trimStart、string.trimEnd 等。
  • ES2020 ES2020 中可用的其他 API,string.matchAll 等。
  • ES2021:ES2021 中可用的其他 API - promise.any、string.replaceAll 等。
  • ESNext:ESNext 中可用的其他 API - 这会随着 JavaScript 规范的发展而变化
  • DOM:DOM 定义 - 窗口、文档等。
  • WebWorker:上下文中可用的 WebWorker API
  • ScriptHost:用于 Windows 脚本宿主系统的 ScriptHost API
    比如有了 dom 定义,我们可以在 Angular 应用里使用 dom 相关的 api:

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK