How to fix error TS2688: Cannot find type definition file for 'element-plus/global'

According to elemnt-plus document, we need the following configuration when use volar:

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "types": ["element-plus/global"]
  }
}

The Problem is that, an error occurs when I do the type checking:

error TS2688: Cannot find type definition file for 'element-plus/global'.
  The file is in the program because:
    Entry point of type library 'element-plus/global' specified in compilerOptions

  tsconfig.app.json:12:15
    12     "types": ["element-plus/global"]
                     ~~~~~~~~~~~~~~~~~~~~~
    File is entry point of type library specified here.

Besides the error above, the vscode IntelliSense for element-plus component also failed.

Solution

Since typescript5.x, element-plus/global cannot be recognized correctly.

You can use a full path so that typescript5.x can find it:

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "types": ["element-plus/global.d.ts"]
  }
}

In simple terms, replace element-plus/global with element-plus/global.d.ts.

Things to note

This article is written when I am using element-plus 2.3.14. In the future, element-plus team may fix this issue and element-plus/global.d.ts is then not necessary.

Posted on 2023-09-20