[英]@types/core-js for a dynamic import of the "core-js/stable"
This is what I do in my index.js
to avoid delivering polyfills to all my users.这就是我在
index.js
中所做的,以避免向所有用户提供 polyfill。
index.js索引.js
const renderApp = () => {
ReactDOM.render(
<App
firebase={firebase}
/>
,document.getElementById("root") as HTMLElement
);
};
/* ############################ */
/* #### CHECK FOR POLYFILL #### */
/* ############################ */
if (
'fetch' in window &&
'Intl' in window &&
'URL' in window &&
'Map' in window &&
'forEach' in NodeList.prototype &&
'startsWith' in String.prototype &&
'endsWith' in String.prototype &&
'includes' in String.prototype &&
'includes' in Array.prototype &&
'assign' in Object &&
'entries' in Object &&
'keys' in Object
) {
renderApp();
} else {
import(
/* webpackChunkName: "core-js-stable" */
/* webpackMode: "lazy" */
'core-js/stable' // <---- DYNAMIC IMPORTED "core-js/stable"
).then(renderApp);
}
But I'm getting the "implicit any type" warning in the dynamic import for the core-js/stable
package. And since I usually work witht the --noImplicitAny
flag, my project won't compile like that.但是我在
core-js/stable
package 的动态导入中收到“隐式任何类型”警告。而且由于我通常使用--noImplicitAny
标志,所以我的项目不会像那样编译。
The weird thing is that I have installed the @types/core-js package
奇怪的是我已经安装了
@types/core-js package
Why is Typescript not finding the types for the core-js package?为什么 Typescript 找不到 core-js package 的类型?
NOTE: If I import it with a regular import "core-js/stable"
I do not get this warning/error.注意:如果我使用常规
import "core-js/stable"
导入它,我不会收到此警告/错误。
Also, if I dynamic import it only the core-js
without the /stable
path, I do not get any warning/error.另外,如果我只动态导入没有
/stable
路径的core-js
,我不会收到任何警告/错误。
Like, this is fine:就像,这很好:
import(
/* webpackChunkName: "core-js-stable" */
/* webpackMode: "lazy" */
'core-js'
).then(renderApp);
tsconfig.json tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"esModuleInterop": true,
"jsx": "react",
"module": "CommonJS",
"sourceMap": true,
"strictNullChecks": true,
"target": "ES5",
"paths": {
"@src/*": ["./src/*"],
}
},
"include": [
"src/**/*"
]
}
Here is what I did to get rid of the error/warning.这是我为消除错误/警告所做的工作。
Got this idea from Typescript docs:从 Typescript 文档中得到这个想法:
https://www.typescriptlang.org/docs/handbook/namespaces.html#working-with-other-javascript-libraries https://www.typescriptlang.org/docs/handbook/namespaces.html#working-with-other-javascript-libraries
I've created this folder for ambient declarations and an index.d.ts
file我已经为环境声明和
index.d.ts
文件创建了这个文件夹
/types/ambient/index.d.ts
index.d.ts索引.d.ts
declare module 'core-js/stable';
And on index.tsx
you've got to reference the d.ts
file that you've created, using triple slash directive.在
index.tsx
上,您必须使用三重斜杠指令引用您创建的d.ts
文件。
index.tsx索引.tsx
/// <reference path="./types/ambient/index.d.ts"/>
(...) OTHER CODE
import(
/* webpackChunkName: "core-js-stable" */
/* webpackMode: "lazy" */
'core-js/stable'
).then(renderApp);
Now everything works without errors / warnings.现在一切正常,没有错误/警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.