最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

TypeScript unable to use find method - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论