簡體   English   中英

Typescript 無法編譯類型不存在的錯誤 TS2339

[英]Typescript can't compile type not existing Error TS2339

嗨,有沒有人可以幫助我解決以下打字稿問題(我對打字稿完全陌生,我正在嘗試將舊的 js(不是我寫過的)文件轉換為打字稿)

到目前為止,我所做的是將舊的 js 復制粘貼到一個新的打字稿文件中。 花幾個小時嘗試各種不同的方法來讓 tsconfig.json 玩得很好,然后讓我編譯新的 ts 文件。 我可以看到運行配置到嚴格時有很多警告,我希望能一一解決問題。 但是現在我被以下錯誤所困擾。

錯誤 TS2339 (TS)屬性 'ajax' 在類型'typeof jQuery | 上不存在 ((w: any) => typeof jQuery)'。 類型“typeof jQuery”上不存在屬性“ajax”。 雜項 當調用這樣的東西時 --> jQuery.ajax 或$.ajax sample.ts 166 Active

在文件的頂部,我有以下內容

import * as jQuery from '../Scripts/jquery-3.6.0.js';
import * as jcookie from '../Scripts/node_modules/js-cookie/dist/js.cookie';

如果我不重命名(as)並嘗試使用 $ 也沒關系

我已經嘗試使用 .js 並且文件末尾沒有 .js 。 我可以在智能感知中看到存在的文件(也只需查看文件夾)

失敗的調用示例代碼在純 javascript 中工作(使用 $ 而不是我重命名為 jQuery)

mListHandler.prototype.addm = function () {
    var ref = this;

    ref.ui.jQueryAddmButton.attr("disabled", "disabled");

    jQuery.ajax({
        "url": ref.addmURL,
        "type": "POST",
        "contentType": "application/json",
        "error": function () {
            ref.ui.jQueryAddmButton.removeAttr("disabled");
            alert("Server not responding. Unable to save!");
        },
        "success": function (data: any) {
            if (!!data) {                
                jQuery.ajax({
                    "url": appendURL("m?mIDs=" + data), 
                    "type": "POST",
                    "success": function (data: any) {
                        ref.ui.jQuerymTable.append(jQuery("tbody", jQuery(data)).html());                        
                        ref.bindTable();
                    }
                });
            }
        }
    });
};

我的 tsconfig.json 文件看起來像這樣

{
  "compileOnSave": true,
  "compilerOptions": {
    "esModuleInterop": true,    
    "allowJs": true,
    "target": "es6",
    "allowUnreachableCode": true,
    "noImplicitReturns": true,
    "noImplicitAny": false,
    "outDir": "./AppScript"
  },
  "include": [ "./TSScripts/*.ts" ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

嘗試像這樣導入 $

import $ from 'jquery'

https://momane.com/how-to-use-jquery-with-typescript

我發現這很有幫助https://www.typescriptlang.org/tsconfig

我希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM