且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Core-JS Map导入angular-cli Webpack生成的应用程序

更新时间:2023-09-28 08:40:16

此处的问题是,core-js的声明重新声明了TypeScript本身提供并默认包含的许多类型.

The issue here is that the declarations for core-js redeclare many types that TypeScript itself provides and includes by default.

core-js旨在多填充ECMAScript标准库的当前和将来规范. TypeScript旨在描述那些当前和将来的规范的类型.

core-js aims to polyfill the current and future specifications of the ECMAScript standard library. TypeScript aims to describe the types of those same current and future specifications.

core-js可用的类型声明因此与该语言提供的类型声明重叠.

The type declarations available for core-js thusly overlap with those provided by the language.

您可以使用一些可靠的选择:

You have a few solid options that you can use:

  1. 在tsconfig.json中,在"compilerOptions"下指定"noLib": true,以指示TypeScript避免在编译上下文中自动包括其内置的标准库声明.
  2. 在tsconfig.json中指定"lib"选项,该选项可让您更精细地控制要包含的标准库声明.例如,您可以指定"lib": ["es2017.symbol.wellknown", "dom"].
  3. 卸载@ types/core-js软件包,并通过指定"lib": ["es2017", "dom"]来简单地使用TypeScript的内置声明文件.您将继续使用core-js,但是将使用TypeScript自己维护良好的定义来 type 实现.
  1. In tsconfig.json specify "noLib": true under "compilerOptions" to instruct TypeScript to refrain from automatically including its built in standard library declarations in the compilation context.
  2. In tsconfig.json specify the "lib" option which gives you more granular control over which standard library declarations are included. For example you might specify "lib": ["es2017.symbol.wellknown", "dom"].
  3. Uninstall the @types/core-js package and simply use TypeScript's built in declaration files as above by specifying "lib": ["es2017", "dom"]. You will continue to use core-js, but you will be using TypeScript's own well-maintained definitions to type the implementation.

我花了一些时间在这些选项之间来回移动之后,发现选项3是最容易维护的.

After spending some time going back and forth between these options myself, I find that option 3 is the easiest to maintain.

请注意,TypeScript自动包含的声明文件取决于tsconfig中的"target"属性.

Please note that the declaration files automatically included by TypeScript depend on the the "target" property in tsconfig.