遇到的一些错误整理

872 阅读2分钟

not assignable to parameter of type ‘Plugin_2’.

  • 报错信息

自定义组件在main.ts中使用use(xxx)注册为全局组件时,编辑器爆红

(alias) const CreateVote: DefineComponent<{}, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin, ComponentOptionsMixin, ... 4 more ..., {}>\
import CreateVote

类型“DefineComponent<{}, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin, ComponentOptionsMixin, ... 4 more ..., {}>”的参数不能赋给类型“Plugin_2”的参数。\
不能将类型“DefineComponent<{}, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin, ComponentOptionsMixin, ... 4 more ..., {}>”分配给类型“{ install: PluginInstallFunction; }”。ts(2345)
  • 解决方案
// vite.env.ts
declare module "*.vue" {
  // import type { DefineComponent } from 'vue'
  //  const component: DefineComponent<{}, {}, any>;
  //  export default component;
  import type { App, DefineComponent } from "vue";
  const component: DefineComponent<{}, {}, any> & {
    install(app: App): void;
  };
  export default component;
}

Module ‘“xx.vue“‘ has no default export.Vetur

  • 自定义组件引入时编辑器爆红
  • 解决方案

vscodeVetur插件卸载 安装volar插件即可

Re-exporting a type

  • 错误信息
Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
  • 解决方案

导出时加type

interface IStreamConfig {
  userId: string;
  audio: boolean;
  microphoneId?: string;
  video: boolean;
  cameraId?: string;
  // 指定使用前置或后置摄像头来采集视频。在移动设备上,可通过该参数选择使用前置或后置摄像头 'user': 前置; 'environment': 后置
  facingMode?: string;
  screen?: boolean;
  screenAudio: boolean;
  audioSource?: MediaStreamTrack;
  videoSource?: MediaStreamTrack;
  mirror?: boolean;
}

export { type IStreamConfig };

add a new declaration (.d.ts) & `declare module 'xxx'

  • 错误信息:
Could not find a declaration file for module 'tim-js-sdk'. 
'F:/new/initpro/node_modules/tim-js-sdk/tim-js.js' implicitly has an 'any' type.
 Try `npm i --save-dev @types/tim-js-sdk` 
 if it exists or add a new declaration (.d.ts) file containing `declare module 'tim-js-sdk';`
  • 解决方案

在src目录下新建types文件夹,在types文件夹下新建index.d.ts,在里面去声明上面错误信息提及的模块。 然后再tsconfig.json文件中includes中添加types ,需重启编辑器vscode

// types/index.d.ts
declare module "tsignaling/tsignaling-js";
declare module "tim-js-sdk";
// tsconfig.json
 "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue","types"],

Cannot find module 'xx' or corresponding type declarations.

  • 错误信息

TypeScript需要对别名进行解析

Cannot find module '@/TUIRoom/stores/basic' or its corresponding type declarations.
import {useBasicStore} from '@/TUIRoom/stores/basic';
  • 解决方案

tsconfig.json中配置baseUrlpath,配置好后需要重启编辑器vscode才会生效 其中pathvite.config.ts中起的别名有关系 且引入的时候不要带.ts alias: { "@": path.resolve(__dirname, "src"), "@TUIRoom": path.resolve(__dirname, "src/TUIRoom"), },

  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
       "@/*": ["src/*"],
      "@TUIRoom/*":["src/TUIRoom/*"]
    },
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM"],
    "skipLibCheck": true
  },

Function lacks ending return statement

  • 错误信息
//(函数缺少结束返回语句 TS 中的错误)
 Function lacks ending return statement and return type does not include 'undefined'.
534   async getMessageList(conversationID: string,count:number=15): Promise<TUIRoomResponse<any>> {

当具有显式返回类型的函数的所有代码路径都未返回值时,
会发生“函数缺少结束返回语句并且返回类型不包含未定义”错误。
若要解决此错误,请从所有代码路径返回一个值,或在函数的返回类型中包含`未定义`
  • 解决方案
若要解决此问题,请使用[联合类型](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types)
将函数的返回类型设置为 `Promise<TUIRoomResponse<any> | undefined>`