我有一个使用 Zod 的 Vite 库。我想解析配置,我的文件夹结构与配置对象结构类似。index.ts
文件始终导出其自己目录中的所有文件及其子目录中的所有内容,例如export * from './subDir';
所以根文件导出“整个库”。
以下设置显示了单个配置分支
Stackblitz 上的示例代码 https://stackblitz.com/edit/vitest-dev-vitest-fchtxz
.
├── src
│ ├── api
│ │ ├── dataSources
| | | ├── dataSource
| | | | ├── following
| | | | | ├── computed
| | | | | | ├── followingComputedDataSourceConfigurationSchema.ts ( extends dataSourceConfigurationSchema )
| │ │ | | | └── index.ts
| | | | | ├── entity
| | | | | | ├── followingEntityDataSourceConfigurationSchema.ts ( extends leadingDataSourceConfigurationSchema )
| │ │ | | | └── index.ts
| │ │ | | └── index.ts
| | | | ├── leading
| | | | | ├── leadingDataSourceConfigurationSchema.ts ( extends dataSourceConfigurationSchema )
| │ │ | | └── index.ts
| | | | ├── dataSourceConfigurationSchema.ts ( base schema )
│ │ | | └── index.ts
| | | ├── dataSourcesConfigurationSchema.ts ( expects leading and array of followings )
│ │ | └── index.ts
| | ├── apiConfigurationSchema.ts ( expects dataSources )
│ │ └── index.ts
│ └── index.ts
└── test
└── basic.test.ts
问题是我认为我遇到了循环依赖项导入。我使用 Vitest 通过测试检查了架构
it('fails.', () => {
expect(() => apiConfigurationSchema.parse({})).not.toThrow();
});
通过这样做我得到以下错误
类型错误:无法读取未定义的属性(读取“_parse”)
我不想将模式合并到一个大文件中,因为子目录可能还包含此特定部分的自定义验证函数。
您对如何修复此设置有任何想法吗?
你有一个循环依赖(在2index.ts
)
// index.ts
export { apiConfigurationSchema } from './apiConfigurationSchema';
export * from './dataSources';
// apiConfigurationSchema.ts
import { dataSourcesConfigurationSchema } from '.';
export const used = __use(dataSourcesConfigurationSchema )
您进口和使用再出口的地方*
在重新出口之前
所以,你需要解决的只是......
// index.ts
export * from './dataSources';
export { apiConfigurationSchema } from './apiConfigurationSchema';
...只是交换两个文件中的两行
https://stackblitz.com/edit/vitest-dev-vitest-8uh3ya?file=src/api/index.ts https://stackblitz.com/edit/vitest-dev-vitest-8uh3ya?file=src/api/index.ts
研究详细信息:我创建了一个文件
// a.ts
import { apiConfigurationSchema } from './src/api/apiConfigurationSchema';
apiConfigurationSchema.parse({});
,已安装tsx
包,跑tsx watch a
,并查找错误所在,直到修复为止
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)