[ts] The ts project introduces the method of file reporting and global type declaration

vue3项目中使用ts,如果要引入的文件没有相应的类型声明文件,那么你需要为每个文件创建对应的类型声明文件
For example: I want to import the index.vue file.

  1. Then you need to create the index.vue.d.ts file in the src directory, and make sure that the file name is consistent with the module file name that needs to create a type declaration.
  2. Write the corresponding type declaration in the index.vue.d.ts file. Depending on the content of your module, you can create an interface for the module's exported objects and specify the corresponding type for each property.
// index.vue.d.ts
declare module '@/Layout/index.vue' {
    
    
  import {
    
     ComponentOptions } from 'vue';
  
  const componentOptions: ComponentOptions;
  
  export default componentOptions;
}

However, it is impossible to import only one file in a project. It is too cumbersome and redundant to create a type declaration file for each file.

就要用统一的方式解决这个问题:使用类型声明文件的全局声明,在项目中创建一个名为global.d.ts的全局类型声明文件,就可以为整个项目中的文件添加类型声明

// global.d.ts
declare module '*.vue' {
    
    
  import {
    
     ComponentOptions } from 'vue';

  const componentOptions: ComponentOptions;

  export default componentOptions;
}

おすすめ

転載: blog.csdn.net/bbt953/article/details/132378732