带有第三方库的 Typescript 3 项目参考(来自 npm)

2024-01-07

我正在使用 Typescript,并尝试与服务工作者建立后台同步。有人建议我应该这样做this: https://stackoverflow.com/a/52377959/6683308

您可以使用单独的 tsconfig.json 文件将项目拆分为多个部分:一部分包含 dom 库,另一部分包含 webworker 库。您可以为不依赖于任一库的通用代码创建另一部分。您可以使用项目引用来帮助自动化构建。如果这不能完全解决问题,请更新问题以描述未解决的问题。

首先,我想承认我可能从根本上滥用项目参考,所以请互联网上的好心人怜悯我。

所以现在我的项目结构如下所示:

root:
├── node_modules
│   └── [..... all the packages]
├── src
│   ├── tsconfig.json
│   └── [other source files]
├── srcWebWorker
│   └── tsconfig.json
├── structure.txt
├── tsconfig.base.json
└── tsconfig.json

在 src/tsconfig.json 中,我使用 lib dom,因为我需要它来处理非服务工作者代码:

{
    "extends": "../tsconfig.base",
    "compilerOptions": {
        "lib": [ "es6", "dom", "esnext"], // ** notice I use "dom" here
    }, 
}

./srcWebworker/tsconfig.json:

{
    "extends": "../tsconfig.base",
    "compilerOptions": {
        // "outFile": "../built/local/tsc.js",
        "lib": [ "webworker" ],
    },
    "files": [

    ],
    "references": [

    ]
}

src/tsconfig.json 所指的 tsconfig.base.json :

{

    "compilerOptions": {
        "baseUrl": ".",
        "outDir": "build/dist",
        "module": "esnext",
        "target": "es6",
        "sourceMap": true,
        "allowJs": true,
        "jsx": "react",
        "moduleResolution": "node",
        "rootDir": "./src",
        "forceConsistentCasingInFileNames": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "suppressImplicitAnyIndexErrors": true,
        "noUnusedLocals": true
    }, 
    "exclude": [
        "node_modules",
        "build",
        "scripts",
        "acceptance-tests",
        "webpack",
        "jest",
        "src/setupTests.ts"
    ]
}

根目录中的 tsconfig.json:

{
    "files": [],
    "include": [],  
    "composite": true,
    "references": [
        { "path": "./src" }
    ]
}

我的理论是 scr/tsconfig 将引用并扩展 tsconfig.base.json,因此 ./src 内的代码将能够使用 --lib“dom”。然而,当我尝试编译时,出现了这个错误:

/Users/shunxu Huang/Projects/hemera/mainapp/node_modules/@types/graphql/subscription/subscribe.d.ts (17,12):找不到名称“AsyncIterator”。

我可以通过添加 lib 来解决此错误dom在根目录的 tsconfig.json 中,但我不希望 srcWebworker 中的代码使用它,因为它与 lib 冲突网络工作者

I tried tsc --build它返回

node_modules/@types/react-dom/index.d.ts(19,72): error TS2304: Cannot find name 'Text'.
node_modules/popper.js/index.d.ts(93,13): error TS2304: Cannot find name 'CSSStyleDeclaration'.
node_modules/popper.js/index.d.ts(94,18): error TS2304: Cannot find name 'CSSStyleDeclaration'.
node_modules/popper.js/index.d.ts(122,30): error TS2304: Cannot find name 'ClientRect'.

边注: 我用了这个样板 https://github.com/wmonk/create-react-app-typescript/创建我的项目,这样我就不必处理网络打包,但如果有必要,我可以弹出。

谢谢:=)


尝试改变lib设置在./srcWebworker/tsconfig.json to ["es6", "webworker", "esnext"]. AsyncIterator定义于esnext.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有第三方库的 Typescript 3 项目参考(来自 npm) 的相关文章

随机推荐