Turborepo Monorepo中配置包调用config.get报类型不存在错误
解决“Property 'get' does not exist on type”错误
错误原因分析
你遇到的错误核心是配置包的默认导出未被正确对外暴露,同时存在一处类型拼写错误(虽不直接触发当前错误,但会影响类型体系的准确性):
- 配置包的
index.ts仅用export *导出命名成员,不会包含默认导出的convict实例config,导致业务代码导入时无法获取到带有get方法的对象。 - 类型定义里的
aipSchema是拼写错误,应为apiSchema,会干扰后续类型提示。
修复步骤
1. 完善配置包的导出逻辑
修改配置包根目录的index.ts,显式导出默认的config实例:
// configuration/index.ts export * from './config/config'; export { default } from './config/config';
2. 修正类型拼写错误
修改config/config.ts中的类型名称:
// 原错误拼写 // export type aipSchema = DeepReadOnly<typeof config extends convict.Config<infer T> ? T : never>; // 修正后 export type apiSchema = DeepReadOnly<typeof config extends convict.Config<infer T> ? T : never>; export type ConfigKey = keyof apiSchema;
3. 验证导入与使用
确保业务代码的导入逻辑正确,此时config.get()方法会被TypeScript正确识别:
import config from "@repo/configuration"; export function generateAccessToken(user: User) { return jwt.sign({ userId: user.id }, config.get('JWT_SECRET'), { expiresIn: '5m', }); }
额外说明
如果问题仍未解决,可确认convict版本(推荐使用v6+,类型定义更完善),或显式指定config的类型:
const config = convict(apiConfigSchema) as convict.Config<typeof apiConfigSchema>;
内容的提问来源于stack exchange,提问作者joshk132
相关产品推荐
相关产品推荐

