请问老师关于tsc编译Promise

来源:12-5 驯服tsc - tsconfig 编写第一部分

hy_wang

2021-10-26

请问下老师我的项目里代码全部使用tsc编译。我想让ts让babel一样可以编译我的Promise拥有Promise的polyfill。查阅官网我发现配置lib库就可以解决。但是我这么配置后发现还是无效。

{
  "exclude": [
    "node_modules",
    "**/*.stories.tsx",
    "**/*.test.tsx"
  ],
  "include": [
    "./lib/"
  ],
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "ESNext", "es5", "es2015.promise"],
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {},
    "allowJs": false,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "isolatedModules": true,
    "alwaysStrict": true,
    "declaration": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "./types"
  }
}

请问老师我应该如何处理呢

写回答

1回答

张轩

2021-10-28

同学你好 lib 和编译出来的代码没有关系。请看问答:https://github.com/microsoft/TypeScript/issues/14308


The lib option is only for typechecking, it doesn't affect codegen.

针对低版本的浏览器不支持 Promise 的话,需要手动的设置 polyfill

这文章写的很好,强烈建议看一下:https://juejin.cn/post/6844903869579788296 正好解决你的问题

0
3
hy_wang
回复
张轩
老师如果使用这在项目中已经使用了babel去增加polyfill,此时我如果在组件库中定义了polyfill。是不是就造成了冗余的引入了。除了让使用者在此调用babel去编译组件的代码。还有更好的方式吗
2021-10-29
共3条回复

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2123 学习 · 959 问题

查看课程