最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+tsc+noEmit導(dǎo)致打包報(bào)TS類型錯(cuò)誤問(wèn)題及解決方法

 更新時(shí)間:2023年10月14日 11:56:40   作者:不叫貓先生  
當(dāng)我們新建vue3項(xiàng)目,package.json文件會(huì)自動(dòng)給我添加一些配置選項(xiàng),這寫(xiě)選項(xiàng)基本沒(méi)有問(wèn)題,但是在實(shí)際操作過(guò)程中,當(dāng)項(xiàng)目越來(lái)越復(fù)雜就會(huì)出現(xiàn)問(wèn)題,本文給大家分享vue+tsc+noEmit導(dǎo)致打包報(bào)TS類型錯(cuò)誤問(wèn)題及解決方法,感興趣的朋友一起看看吧

背景

當(dāng)我們新建vue3項(xiàng)目,package.json文件會(huì)自動(dòng)給我添加一些配置選項(xiàng),這寫(xiě)選項(xiàng)基本沒(méi)有問(wèn)題,但是在實(shí)際操作過(guò)程中,當(dāng)項(xiàng)目越來(lái)越復(fù)雜就會(huì)出現(xiàn)問(wèn)題。本文列舉一個(gè)目前我遇到的一個(gè)問(wèn)題:打包后報(bào)了一堆TS類型錯(cuò)誤,怎么消除這些錯(cuò)誤?

項(xiàng)目環(huán)境:Vue3 + Vite + TS

問(wèn)題描述

當(dāng)項(xiàng)目進(jìn)行打包時(shí)候,突然發(fā)現(xiàn)終端有幾十項(xiàng)報(bào)錯(cuò)

npm run build

詳細(xì)報(bào)錯(cuò)信息如下:

src/view/testDemo/index.vue:6:9 - error TS2339: Property 'proxy' does not exist on type 'ComponentInternalInstance | null'.

6 const { proxy } = getCurrentInstance();
          ~~~~~

src/view/echarts/index.vue:7:9 - error TS2339: Property 'proxy' does not exist on type 'ComponentInternalInstance | null'.

7 const { proxy } = getCurrentInstance();
          ~~~~~

src/view/webRTC/index.vue:5:23 - error TS2322: Type 'string | number' is not assignable to type 'string | undefined'.
  Type 'number' is not assignable to type 'string'.

5                 <img :src=" item" alt="" />
                        ~~~

  node_modules/@vue/runtime-dom/dist/runtime-dom.d.ts:616:3
    616   src?: string
          ~~~
    The expected type comes from property 'src' which is declared here on type 'ElementAttrs<ImgHTMLAttributes>'

src/view/webRTC/index.vue:13:55 - error TS2322: Type '(deviceId: string) => Promise<void>' is not assignable to type '(evt: MouseEvent) => any'.
  Types of parameters 'deviceId' and 'evt' are incompatible.
    Type 'MouseEvent' is not assignable to type 'string'.

13             <el-button type="primary" size="default" @click="handleDeviceChange">
                                                         ~~~~~

src/layout/index.vue:14:22 - error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & Partial<{ readonly disabled: boolean; readonly showTimeout: number; readonly hideTimeout: number; readonly popperAppendToBody: EpPropMergeType<BooleanConstructor, unknown, unknown>; readonly popperOffset: number; }> & Omit<...>'.
  Property 'index' is missing in type '{}' but required in type 'Omit<Readonly<ExtractPropTypes<{ readonly index: { readonly type: PropType<string>; readonly required: true; readonly validator: ((val: unknown) => boolean) | undefined; __epPropKey: true; }; readonly showTimeout: EpPropFinalized<...>; ... 8 more ...; readonly collapseOpenIcon: { ...; }; }>> & VNodeProps & AllowedCo...'.

14                     <el-sub-menu>
                        ~~~~~~~~~~~

  node_modules/element-plus/es/components/menu/index.d.ts:363:14
    363     readonly index: {
                     ~~~~~
    'index' is declared here.

src/view/testDemo/index.vue:27:32 - error TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'.
  Type 'null' is not assignable to type 'HTMLElement'.

27     var myChart = echarts.init(document.getElementById("trendLIne-content"));
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/view/testDemo/index_.vue:41:40 - error TS2552: Cannot find name 'myData'. Did you mean 'data'?

41                                 value: myData[dataIndex],
                                          ~~~~~~

  src/view/testDemo/index_.vue:7:5
    7 let data = reactive([
          ~~~~
    'data' is declared here.

src/view/echarts/index.vue:40:32 - error TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'.
  Type 'null' is not assignable to type 'HTMLElement'.

40     var myChart = echarts.init(document.getElementById("trendLIne-content"));
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/view/testDemo/index.vue:42:40 - error TS2552: Cannot find name 'myData'. Did you mean 'data'?

42                                 value: myData[dataIndex],
                                          ~~~~~~

  src/view/testDemo/index.vue:7:5
    7 let data = reactive([
          ~~~~
    'data' is declared here.

src/layout/index.vue:65:20 - error TS2339: Property '$fil' does not exist on type 'ComponentPublicInstance<{}, {}, {}, {}, {}, {}, {}, {}, false, ComponentOptionsBase<any, any, any, any, any, any, any, any, any, {}, {}, string>, {}>'.

65 console.log(proxy?.$fil, "proxy");
                      ~~~~

src/view/webRTC/index.vue:136:5 - error TS2322: Type '{ facingMode: string; }' is not assignable to type 'boolean'.

136     constraints.video = {
        ~~~~~~~~~~~~~~~~~

src/view/webRTC/index.vue:146:13 - error TS2304: Cannot find name 'ElMessage'.

146             ElMessage.success("切換成功");
                ~~~~~~~~~

src/view/webRTC/index.vue:150:13 - error TS2304: Cannot find name 'ElMessage'.

150             ElMessage.error("你的設(shè)備不支持切換前后攝像頭");
                ~~~~~~~~~

src/view/webRTC/index.vue:156:28 - error TS2304: Cannot find name 'multiavatar'.

156     const blob = new Blob([multiavatar(val + new Date().getTime())], {
                               ~~~~~~~~~~~

src/view/testDemo/index.vue:168:9 - error TS1117: An object literal cannot have multiple properties with the same name.

168         grid: {
            ~~~~

src/directives/canvasMakeWaterMark.ts:30:3 - error TS18047: 'ctx' is possibly 'null'.

30   ctx.rotate((rotate * Math.PI) / 180);
     ~~~

src/directives/canvasMakeWaterMark.ts:31:3 - error TS18047: 'ctx' is possibly 'null'.

31   ctx.font = "16px normal";
     ~~~

src/directives/canvasMakeWaterMark.ts:32:3 - error TS18047: 'ctx' is possibly 'null'.

32   ctx.fillStyle = "rgba(180, 180, 180, 0.3)";
     ~~~

src/directives/canvasMakeWaterMark.ts:33:3 - error TS18047: 'ctx' is possibly 'null'.

33   ctx.textAlign = "left";
     ~~~

src/directives/canvasMakeWaterMark.ts:34:3 - error TS18047: 'ctx' is possibly 'null'.

34   ctx.textBaseline = "middle";
     ~~~

src/directives/canvasMakeWaterMark.ts:35:3 - error TS18047: 'ctx' is possibly 'null'.

35   ctx.fillText('請(qǐng)勿外傳', canvas.width / 3, canvas.height / 2);
     ~~~

src/directives/canvasMakeWaterMark.ts:59:26 - error TS2554: Expected 0 arguments, but got 1.

59   const url = getDataUrl(binding);
                            ~~~~~~~

src/directives/canvasMakeWaterMark.ts:68:3 - error TS18047: 'parentElement' is possibly 'null'.

68   parentElement.setAttribute("style", "position: relative;");
     ~~~~~~~~~~~~~

src/directives/canvasMakeWaterMark.ts:71:3 - error TS18047: 'parentElement' is possibly 'null'.

71   parentElement.appendChild(waterMark);
     ~~~~~~~~~~~~~

src/directives/canvasMakeWaterMark.ts:81:23 - error TS18047: 'el.parentElement' is possibly 'null'.

81   const waterMarkEl = el.parentElement.querySelector(".water-mark");
                         ~~~~~~~~~~~~~~~~

src/directives/canvasMakeWaterMark.ts:87:25 - error TS18047: 'waterMarkEl' is possibly 'null'.

87       const currStyle = waterMarkEl.getAttribute("style");
                           ~~~~~~~~~~~

src/directives/canvasMakeWaterMark.ts:116:20 - error TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Node'.
  Type 'null' is not assignable to type 'Node'.

116   observer.observe(el.parentElement, {
                       ~~~~~~~~~~~~~~~~

截圖圖片如下:

分析

打包執(zhí)行的是npm run build命令,那么具體執(zhí)行了什么呢?我們查看package.json中配置項(xiàng)。

  "scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview"
  },

可以發(fā)現(xiàn)在build時(shí),執(zhí)行了vue-tsc --noEmit && vite build,其中

  • vue-tsc:Vue 官方提供的命令,用于執(zhí)行 TS 的類型檢查。它在執(zhí)行時(shí)會(huì)根據(jù)項(xiàng)目中的 tsconfig.json 文件配置進(jìn)行類型檢查
  • --noEmit:TS 編譯器的選項(xiàng),使用 --noEmit 選項(xiàng)后,編譯器僅執(zhí)行類型檢查,而不會(huì)生成任何實(shí)際的編譯輸出

所以可以看出了,在打包的時(shí)候編譯器執(zhí)行了TS類型檢查,所以才報(bào)了一堆錯(cuò),類型錯(cuò)誤最終不會(huì)影響項(xiàng)目的正常運(yùn)行

解決

根據(jù)上面分析,package.json中的"scripts"修改如下:

  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },

另外項(xiàng)目通常在編譯時(shí)候,也會(huì)對(duì)我們引入的一些依賴包進(jìn)行類型校驗(yàn),我們并不需要這個(gè)操作,所以可以在tsconfig.json中設(shè)置如下:

 "compilerOptions": {
    "skipLibCheck": true
  },

設(shè)置后編譯器不會(huì)檢查庫(kù)文件中的類型定義是否正確,也不會(huì)對(duì)庫(kù)文件的使用進(jìn)行類型檢查。

再次打包就非常順利~

到此這篇關(guān)于vue-tsc --noEmit導(dǎo)致打包報(bào)TS類型錯(cuò)誤的文章就介紹到這了,更多相關(guān)vue-tsc --noEmit打包報(bào)TS錯(cuò)誤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue 配合eiement動(dòng)態(tài)路由,權(quán)限驗(yàn)證的方法

    Vue 配合eiement動(dòng)態(tài)路由,權(quán)限驗(yàn)證的方法

    今天小編就為大家分享一篇Vue 配合eiement動(dòng)態(tài)路由,權(quán)限驗(yàn)證的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 如何使用Vue做個(gè)簡(jiǎn)單的比較兩個(gè)數(shù)字大小頁(yè)面

    如何使用Vue做個(gè)簡(jiǎn)單的比較兩個(gè)數(shù)字大小頁(yè)面

    這篇文章主要給大家介紹了關(guān)于如何使用Vue做個(gè)簡(jiǎn)單的比較兩個(gè)數(shù)字大小頁(yè)面的相關(guān)資料,實(shí)現(xiàn)一個(gè)比較兩個(gè)數(shù)字大小的頁(yè)面,練習(xí)Vue實(shí)例的創(chuàng)建、數(shù)據(jù)綁定和事件監(jiān)聽(tīng)方法,需要的朋友可以參考下
    2023-10-10
  • elementUI Pagination 分頁(yè)指定最大頁(yè)的問(wèn)題及解決方法(page-count)

    elementUI Pagination 分頁(yè)指定最大頁(yè)的問(wèn)題及解決方法(page-count)

    項(xiàng)目中遇到數(shù)據(jù)量大,查詢的字段多,但用戶主要使用的是最近的一些數(shù)據(jù),1萬(wàn)條以后的數(shù)據(jù)一般不使用,這篇文章主要介紹了elementUI Pagination 分頁(yè)指定最大頁(yè)的問(wèn)題及解決方法(page-count),需要的朋友可以參考下
    2024-08-08
  • Vue3源碼通過(guò)render?patch?了解diff

    Vue3源碼通過(guò)render?patch?了解diff

    這篇文章主要為大家介紹了Vue3源碼系列通過(guò)render及patch了解diff原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Vue中設(shè)置登錄驗(yàn)證攔截功能的思路詳解

    Vue中設(shè)置登錄驗(yàn)證攔截功能的思路詳解

    今天在做vue和springboot交互的一個(gè)項(xiàng)目的時(shí)候,想要基于前端實(shí)現(xiàn)一些只有登錄驗(yàn)證之后才能訪問(wèn)某些頁(yè)面的操作,所以在這里總結(jié)一下實(shí)現(xiàn)該功能的一個(gè)解決方案
    2021-10-10
  • 探索?Vue.js?組件的最新特性(最新整理)

    探索?Vue.js?組件的最新特性(最新整理)

    本文將深入探討 Vue.js 組件的一些最新特性,包括組合式 API、Teleport、Suspense 等,幫助開(kāi)發(fā)者更好地掌握和運(yùn)用這些特性,從而構(gòu)建出更加高效、復(fù)雜的前端應(yīng)用,感興趣的朋友一起看看吧
    2025-07-07
  • Vue開(kāi)發(fā)實(shí)現(xiàn)滑動(dòng)驗(yàn)證組件

    Vue開(kāi)發(fā)實(shí)現(xiàn)滑動(dòng)驗(yàn)證組件

    這篇文章主要為大家詳細(xì)介紹了如何利用Vue開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單的滑動(dòng)驗(yàn)證組件,并且適配移動(dòng)和PC,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-07-07
  • vue多頁(yè)面項(xiàng)目中路由使用history模式的方法

    vue多頁(yè)面項(xiàng)目中路由使用history模式的方法

    這篇文章主要介紹了vue多頁(yè)面項(xiàng)目中路由如何使用history模式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 詳解Vue3?父組件調(diào)用子組件方法($refs?在setup()、<script?setup>?中使用)

    詳解Vue3?父組件調(diào)用子組件方法($refs?在setup()、<script?setup>?中使用)

    這篇文章主要介紹了Vue3?父組件調(diào)用子組件方法($refs?在setup()、<script?setup>?中使用),在 vue2 中 ref 被用來(lái)獲取對(duì)應(yīng)的子元素,然后調(diào)用子元素內(nèi)部的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 淺談Vue使用Elementui修改默認(rèn)的最快方法

    淺談Vue使用Elementui修改默認(rèn)的最快方法

    這篇文章主要介紹了淺談Vue使用Elementui修改默認(rèn)的最快方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12

最新評(píng)論

四会市| 黑龙江省| 武隆县| 郁南县| 洮南市| 洛宁县| 道真| 明星| 沂源县| 盐池县| 北流市| 浠水县| 盱眙县| 建昌县| 松滋市| 会泽县| 大田县| 织金县| 韶山市| 铁力市| 大丰市| 林周县| 莎车县| 仁寿县| 灵武市| 皮山县| 林芝县| 九龙城区| 罗田县| 隆化县| 内丘县| 丹寨县| 吉首市| 刚察县| 饶平县| 鄂尔多斯市| 黑水县| 芒康县| 商水县| 湛江市| 绥芬河市|