简体   繁体   English

打字稿处理编译错误

[英]typescript handling compile errors

I have a simple typescript program -我有一个简单的打字稿程序 -

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");

When I compile this program, I get this error -当我编译这个程序时,我收到这个错误 -

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.

Even though there is a error, I see the output file being generated functions.js.即使有错误,我也看到正在生成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"; });

From javascript perspective this is correct code.从javascript的角度来看,这是正确的代码。

Should the typescript not generate the output at all until I fix all the errors?在我修复所有错误之前,打字稿是否应该根本不生成输出?

Array.prototype.find was first available with ES2015. Array.prototype.find最初在 ES2015 中可用。

Thus, you need to tell TypeScript that you are using ES2015.因此,您需要告诉 TypeScript 您正在使用 ES2015。 You can use the --lib compiler option: TypeScript doc .您可以使用--lib编译器选项: TypeScript doc

You can also use the "lib" config in tsconfig.json , for instance:您还可以在tsconfig.json使用“lib”配置,例如:

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

(Consider looking at each configuration in the documentation so you perfectly know what the Compiler is told to do). (考虑查看文档中的每个配置,以便您完全了解编译器被告知要做什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM