Claude Skills Guide

Claude Code Not Recognizing TypeScript Path Aliases in tsconfig: Fix Guide

When working with TypeScript projects that use path aliases (like @components/* or @lib/*), you may encounter a situation where Claude Code generates incorrect import paths or fails to recognize your configured aliases. This creates friction during development, especially in larger codebases where path aliases improve code organization and maintainability.

This guide provides practical solutions to ensure Claude Code properly recognizes and respects your TypeScript path alias configurations.

Understanding the Problem

TypeScript path aliases allow you to define shortcut paths in your tsconfig.json instead of using relative imports:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@lib/*": ["src/lib/*"],
      "@utils/*": ["src/utils/*"]
    }
  }
}

With this configuration, you can write:

import Button from '@components/Button';
import { formatDate } from '@utils/date';

Instead of:

import Button from '../../../components/Button';
import { formatDate } from '../../../../utils/date';

When Claude Code does not recognize these aliases, it may generate relative imports that are harder to maintain, or fail to resolve existing alias imports when reading your code.

Root Causes

Several factors can cause Claude Code to miss your TypeScript path aliases:

  1. Missing type definitions: The TypeScript language server may not have the proper types loaded for path resolution
  2. Build tool configuration mismatch: Your bundler (Vite, Webpack, Rollup) may not have the corresponding alias configuration
  3. Project structure issues: Claude Code may not be analyzing the correct tsconfig.json
  4. Language server restart needed: The TypeScript language server needs to reload after configuration changes

Solutions

Solution 1: Verify Your tsconfig.json Structure

Ensure your tsconfig.json has the correct path alias configuration:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

The baseUrl must point to the directory containing the paths defined in paths. Place this in your root tsconfig.json or in a tsconfig.json that extends it.

Solution 2: Install ts-node and Ensure Type Resolution

If you are using Node.js tools, install the necessary TypeScript resolution packages:

npm install --save-dev tsconfig-paths typescript

Create a tsconfig.json that includes path resolution support:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

Solution 3: Configure Your Bundler to Match

For Vite projects, update vite.config.ts:

import { defineConfig } from 'vite';
import path from 'path';

export default defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
});

For Next.js projects, use next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    config.resolve.alias = {
      ...config.resolve.alias,
      '@': require('path').resolve(__dirname, './src'),
    };
    return config;
  },
};

module.exports = nextConfig;

Solution 4: Restart the TypeScript Language Server

Claude Code relies on the TypeScript language server for code intelligence. After modifying your tsconfig.json:

  1. Run the TypeScript: Restart TS Server command in VS Code
  2. Or create a new Claude Code session to pick up the updated configuration

Solution 5: Provide Explicit Context to Claude Code

When working with Claude Code, you can explicitly reference your path aliases in your prompts:

I'm using TypeScript path aliases: '@components/*' maps to 'src/components/*'.
When generating imports, please use these aliases instead of relative paths.

You can add this to your project’s CLAUDE.md file for persistent context:

# Project Path Aliases

This project uses TypeScript path aliases:
- `@components/*``src/components/*`
- `@lib/*``src/lib/*`
- `@utils/*``src/utils/*`
- `@/*``src/*`

Please use these aliases in all generated imports.

Integration with Claude Skills

Several Claude Code skills can help manage path alias configurations:

When using these skills, reference your path aliases in the skill invocation:

/frontend-design Create a user profile card component using @components/* aliases
/tdd Write tests for the auth module using @lib/* aliases

Common Configuration Mistakes

Avoid these frequent errors when setting up path aliases:

  1. Forgetting baseUrl: The paths configuration requires baseUrl to be set
  2. Incorrect wildcard usage: Use * only once per path pattern
  3. Mismatched directories: Ensure the mapped directories actually exist
  4. Conflicting configurations: Multiple tsconfig.json files may have conflicting settings

Testing Your Configuration

Verify that your path aliases work correctly:

# Check TypeScript compilation with aliases
npx tsc --noEmit

# Verify imports resolve correctly
npx tsc --traceResolution

The --traceResolution flag shows how TypeScript resolves each import, helping you identify configuration issues.

Summary

Claude Code recognition of TypeScript path aliases requires proper configuration in both your tsconfig.json and your build tool. The key steps are:

  1. Define path aliases in tsconfig.json with correct baseUrl setting
  2. Match the aliases in your bundler configuration (Vite, Webpack, Next.js)
  3. Restart the TypeScript language server after changes
  4. Provide explicit context to Claude Code about your aliases via CLAUDE.md

By ensuring consistency across your TypeScript and build configurations, Claude Code will correctly recognize and use your path aliases throughout your project.


Built by theluckystrike — More at zovo.one