admin管理员组

文章数量:1332389

I have this code in my TypeScript file:

class Template {
  private templates = [
    {
      name: 'other',
      template: `<h1>Template</h1>`
    }
 ];
        
 public getByName(name: string) {
   return this.templates.find(t => t.name === name)
 }
}
    
const t = new Template();
t.getByName('other');

When I try to compile it, I get this error:

error TS2550: Property 'find' does not exist on type '{ name: string; template: string; }[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

However my lib compiler option is already set like this:

"compilerOptions": {
  "target": "es2016",                                  
  "lib": ["es2016", "dom"],
  ...
}

I just need the array find method in TypeScript, I don't know what else to do to get it.

本文标签: TypeScript unable to use find methodStack Overflow