简体   繁体   中英

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.

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.

Should the typescript not generate the output at all until I fix all the errors?

Array.prototype.find was first available with ES2015.

Thus, you need to tell TypeScript that you are using ES2015. You can use the --lib compiler option: TypeScript doc .

You can also use the "lib" config in tsconfig.json , for instance:

{
  "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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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