在我的包目录中使用turborepo设置,我已经为我的Shadcn组件声明了一个
/utils
目录,但是当我调用函数时:
import {cn} from '@utils'
我收到错误:
找不到模块“@utils”或其相应的类型声明。
但是相对路径有效:
import {cn} from '../../utils'
在组件文件中设置为
packages/shadcn/Component/index.tsx
路径
packages/utils/index.tsx
代码正确:
import {type ClassValue, clsx} from 'clsx'
import {twMerge} from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
packages/typescript-config/
base.json:
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"composite": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"inlineSources": false,
"isolatedModules": true,
"module": "ESNext",
"moduleResolution": "Bundler",
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"baseUrl": ".",
"paths": {
"@/*": ["./*"],
"@utils/*": ["./packages/utils/*"]
}
},
"exclude": ["node_modules"]
}
package.json:
{
"name": "@repo/typescript-config",
"version": "1.0.0",
"private": true,
"publishConfig": {
"access": "public"
},
"devDependencies": {
"typescript": "5.5.4"
}
}
nextjs.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./base.json",
"compilerOptions": {
"plugins": [{"name": "next"}],
"allowJs": true,
"declaration": false,
"declarationMap": false,
"incremental": true,
"jsx": "preserve",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"resolveJsonModule": true,
"strict": true,
"target": "es5"
},
"include": ["src", "next-env.d.ts"],
"exclude": ["node_modules"]
}
尝试在 VSC 中重新启动 TypeScript,但仍然抛出错误
在我的 Turborepo 中,如何让 TypeScript Alias 路径在我的存储库中工作?