以下设置适用于所有应用,但不适用于库...
我在根目录中有 index.d.ts:
// /index.d.ts
declare global {
namespace MyNamespace {
interface MyInterface { ... }
}
}
在 tsconfig.base.json 中,我在 compilerOptions 中添加了 "esModuleInterop": true 和 "typeRoots": ["index.d.ts"]。
然后在每个 /apps/my-app/tsconfg.json 中,我在包含数组中添加了“../../index.d.ts”。
这允许我在应用程序中使用全局声明,而无需导入任何内容。
const myVar: MyNamespace.MyInterface = ...
我尝试对库做同样的事情,但它不起作用。
lib目录下有3个tsconfig.json:
- tsconfig.json(主)
- tsconfig.lib.json (lib)
- tsconfig.spec.json(规范)
- 如果我使用故事书作为库,那么还有第四个 /.storybook/tsconfig.json(故事书)。
lib、spec 和 storybook 配置扩展了主配置。
文件有错误:
Referenced project '/.../libs/shared-one/tsconfig.lib.json' must have setting "composite": true.
如果我将 lib、spec、storybook 配置的复合设置为 true,则会出现以下错误:
Composite projects may not disable declaration emit.
怎么了?如何在 nx 库之间共享全局类型声明?我希望这能开箱即用。
tsconfig.lib.json (lib) 示例
{
"extends": "./tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "../../dist/out-tsc",
"types": ["node"]
},
"files": [
"../../node_modules/@nrwl/react/typings/cssmodule.d.ts",
"../../node_modules/@nrwl/react/typings/image.d.ts"
],
"exclude": [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.stories.ts",
"**/*.stories.js",
"**/*.stories.jsx",
"**/*.stories.tsx"
],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
/libs/my-lib tsconfig.json(主)
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
},
"files": [],
"include": ["../../index.d.ts"],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
},
{
"path": "./.storybook/tsconfig.json"
}
]
}
tsconfig.base.json: (基础)
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@my-org/shared-one": ["libs/shared-one/src/index.ts"],
"@my-org/shared-two": ["libs/shared-two/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"],
"include": ["index.d.ts"]
}