vue js项目升级typescript

vue js项目升级typescript

先装包

npm i typescript ts-loader @types/webpack-env -D

创建tsconfig.json文件

{
 "compilerOptions": {
   "sourceMap": true,
   "strict": true
 },
 "include": ["src/**/*"],
}

创建类型声明文件index.d.ts

declare module '*.vue'{
  import Vue from 'vue'
  export default Vue
} // 解决引入vue文件报错 Cannot find module 'xxx.vue' or its corresponding type declarations.
declare const weixin:any // 解决使用全局变量报错 Cannot find name 'xxx'
interface DateConstructor{ // 拓展Date构造函数上的方法 
   getDateByAge:Function, // 解决Property 'prop' does not exist on type 'typeXXX'. 此类报错
}
interface Date{ // 拓展Date实例上的方法
  toFormat:Function
}

配置webpack

  1. 修改入口文件配置 entry

    比如原入口文件为 ./src/index.js 修改为 ./src/index.ts

  2. 配置loader
     {
       test: /\.ts$/,
         loader: 'ts-loader',
         options: { appendTsSuffixTo: [/\.vue$/] }
     },
    
  3. 配置resolve,添加ts
    resolve: {
      // ...
    	extensions: ['.ts', '.js', '.vue', '.json'],
      // ...
    }
    

搬砖从文件重命名开始

  1. 将你的js文件后缀名改为ts
  2. 打开vue文件,将script部分 lang=”javascript“(如有) 改为 lang=”ts“
    <style>...</style>
    <template>...</template>
    <script lang="ts">
      // ...code
    </script>
    
  3. 根据报错提示,修改代码。(字少事多坑大,手动滑稽)

notes

  1. 此方法仅适用用旧的js项目升级ts

  2. 未引入eslint

  3. 对于老项目来说,可以渐进式升级。部分使用ts,部分使用js。

  4. ts文件内引入js时会报错并提示 无法找到模块“./xxx”的声明文件。“/path/xxx.js”隐式拥有 "any" 类型。。在类型声明文件中添加一条声明 declare module '*/xxx',xxx为你的js模块名。

  5. 在老项目中可能有部分公共模块,还在使用,可能由于历史原因,改造成本较大。可通过添加对应的类型声明文件,声明模块的方法,做到类型提示。例,现有一个 src/common/http文件,新建util.d.ts文件

    declare module '*/common/util'{
      interface requestParams<T>{
        method:"get"|"post"|"delete"|'put'|string,
        url:string,
        data:T
      }
      export default class{
        post <requestT,responseT>(params:requestParams<requestT>):Promise<responseT>
        get <requestT,responseT>(params:requestParams<requestT>):Promise<responseT>
      }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中使用TypeScript可以带来许多好处。首先,TypeScriptJavaScript的一个超集,它提供了类型系统和对ES6的支持,这可以增加代码的可读性和可维护性。在Vue3中,官方已经使用TypeScript编写了源码,这也是Vue3升级的一个亮点。\[1\] 在Vue3中,你可以直接使用TypeScript来定义组件。你可以使用`defineComponent`函数来定义组件,并使用`PropType`来定义组件的属性类型。例如,你可以这样写一个组件: ```typescript import { defineComponent, PropType } from 'vue'; interface Student { name: string; class: string; age: number; } const Component = defineComponent({ props: { success: { type: String }, callback: { type: Function as PropType<() => void> }, student: { type: Object as PropType<Student>, required: true } }, data() { return { message: 'Vue3 code style' }; }, computed: { reversedMessage(): string { return this.message.split('').reverse().join(''); } } }); ``` 这样,你就可以在Vue3中使用TypeScript来定义组件的属性类型,并且享受到类型检查的好处。\[2\] 当然,在初期使用TypeScript时,一些开发者可能会倾向于使用`any`类型,这样会失去TypeScript的类型检查的意义。但是,养成写类型的习惯是很重要的,可以逐渐提高代码的可靠性和可维护性。所以,在使用Vue3和TypeScript时,建议尽量避免使用`any`类型,而是养成写类型的好习惯。\[3\] #### 引用[.reference_title] - *1* *2* *3* [Vue3.0+TS 项目上手教程](https://blog.csdn.net/Ed7zgeE9X/article/details/109039793)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值