Typescript 处理编译错误的类型脚本

Typescript 处理编译错误的类型脚本,typescript,Typescript,我有一个简单的打字脚本程序- 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/typescri

我有一个简单的打字脚本程序-

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的角度来看,这是正确的代码

在我修复所有错误之前,typescript是否应该根本不生成输出?

是ES2015首次提供的

因此,您需要告诉TypeScript您正在使用ES2015。您可以使用
--lib
编译器选项:

您还可以使用
tsconfig.json
中的“lib”配置,例如:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "skipLibCheck": true,
    "lib": ["es2015"]
  }
}
(请考虑查看文档中的每个配置,以便您完全了解编译器要执行的操作)。

似乎运行良好:共享您的打字,甚至是typescript配置,以便我们可以帮助您