# 一.常见问题与解决方案

前言

# 1.TS2345

  • TS2345: Argument of type 'typeof import("/xxx/node_modules/vue/dist/vue")' is not assignable to parameter of type 'Component<any, any, any, ComputedOptions, MethodOptions, {}, any>'.
declare module "*.vue" {
  import type { DefineComponent } from "vue"
  const component: DefineComponent<{}, {}, any>
  export default component
}
1
2
3
4
5

# 2.ts7006

  • 参数“Vue”隐式具有“any”类型。ts(7006)
declare module '*.vue' {
    import { ComponentOptions } from 'vue'
    const componentOptions: ComponentOptions
    export default componentOptions
}
1
2
3
4
5

# 1.模块解析失败

现象:Error: Cannot find module '@/components/Button'。

原因:未正确配置 baseUrl 和 paths。

解决

"baseUrl": ".",
"paths": {
  "@/*": ["./src/*"]
}
1
2
3
4

# 2.JSX 语法报错

现象:Parsing error: Unexpected token。

原因:未配置 jsx 或 jsxFactory。

解决:

"jsx": "react",
"jsxFactory": "React.createElement"
1
2

# 3.装饰器报错

现象:Experimental support for decorators is a feature that is subject to change。

原因:未启用 experimentalDecorators。

解决:

"experimentalDecorators": true,
"emitDecoratorMetadata": true
1
2

# 4.类型检查过严

现象:Variable 'x' implicitly has type 'any'。

原因:noImplicitAny 启用。

解决:

显式声明类型:let x: number;

或局部禁用:// @ts-ignore(不推荐)。