簡體   English   中英

打字稿處理編譯錯誤

[英]typescript handling compile errors

我有一個簡單的打字稿程序 -

const users = [{ name: "Ahmed" }, { name: "Gemma" }, { name: "Jon" }];

// We're going to look to see if we can find a user named "jon".
const jon = users.find(u => u.name === "jon");

當我編譯這個程序時,我收到這個錯誤 -

p2@6190:~/projects/typescript$ tsc functions.ts
functions.ts:4:19 - error TS2339: Property 'find' does not exist on type '{ name: string; }[]'.

4 const jon = users.find(u => u.name === "jon");
                    ~~~~


Found 1 error.

即使有錯誤,我也看到正在生成functions.js 的輸出文件。

var users = [{ name: "Ahmed" }, { name: "Gemma" }, { name: "Jon" }];
// We're going to look to see if we can find a user named "jon".
var jon = users.find(function (u) { return u.name === "jon"; });

從javascript的角度來看,這是正確的代碼。

在我修復所有錯誤之前,打字稿是否應該根本不生成輸出?

Array.prototype.find最初在 ES2015 中可用。

因此,您需要告訴 TypeScript 您正在使用 ES2015。 您可以使用--lib編譯器選項: TypeScript doc

您還可以在tsconfig.json使用“lib”配置,例如:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "skipLibCheck": true,
    "lib": ["es2015"]
  }
}

(考慮查看文檔中的每個配置,以便您完全了解編譯器被告知要做什么)。

暫無
暫無

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

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