.includes类型'字符串[]'

时间:2017-04-26 09:46:53

标签: angular typescript

尝试在我的component.ts文件中使用.include,但它会引发以下错误。帮助我解决问题。

  • 错误
  

财产&#39>包括'类型'字符串[]'

上不存在
  • app.component.ts

    permissionCookie: string[]; 
    this.permissionCookie = cookieService.get("permissions").split(",");
    this.permissionFlag = this.permissionCookie.includes("22");
    
  • ts.config

    {
      "compilerOptions": {
        "baseUrl": "",
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": ["es6", "dom"],
        "mapRoot": "./",
        "module": "es6",
        "moduleResolution": "node",
        "outDir": "../dist/out-tsc",
        "sourceMap": true,
        "target": "es5",
        "typeRoots": [
          "../node_modules/@types"
        ]
      }
    }
    

    Property 'includes' does not exist on type 'string[]'对我没有任何帮助

2 个答案:

答案 0 :(得分:0)

尝试将其声明为,

 private permissionCookie:Array<string>;
constructor() {
  let a = this.permissionCookie.includes("22");
  console.log(a);
}

tsconfig.json - &gt;

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    // "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    // "noImplicitAny": true,
    // "suppressImplicitAnyIndexErrors": true,
    "types": ["jquery"]
  },
  "exclude": [
    "node_modules/*",
    "**/*-aot.ts"
  ]
}

答案 1 :(得分:-1)

"ES2017"添加到"lib"中的tsconfig.json数组:

{
  "compilerOptions": {
    ...
    "lib": ["dom", "es2017"],
    ...
  }
}

这应该适用于TypeScript 2.1 +。

A related issue