【问题标题】:.includes does not exist on type 'string[]'.includes 在类型“字符串 []”上不存在
【发布时间】:2017-09-23 15:29:15
【问题描述】:

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

  • 错误

类型“string[]”上不存在属性“includes”

  • 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[]' 也没有帮助我

【问题讨论】:

  • this.permissionCookie - 这是一个数组吗?包含仅适用于数组。
  • 您说您发布的链接中的解决方案不起作用,但您的配置根本没有使用它。它说使用es2016 作为目标而你不是。当includes 是 ES2016 的一部分时,其他所有内容都设置为 ES6。
  • 是的,它是...@RemyaJ
  • @GillesC 我的意思是我尝试将目标更改为 es2016 的方式,尽管它没有工作

标签: angular typescript


【解决方案1】:

尝试将其声明为,

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

tsconfig.json -->

{
  "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"
  ]
}

【讨论】:

  • 你能在拆分和显示后控制台你的 this.permissionCookie 吗?
  • [ "32", "33", "21", "22" ]
  • 不确定,但是嘿,这对我有用 ` "lib": ["es7", "dom"] ` 现在在我的 ts.config 文件中更改为这个 .includes, .splice 一切正常,没有错误.感谢支持@RemyaJ
  • 你可以发布你的config.ts文件内容@RemyaJ
  • 已添加,您可以查看。
【解决方案2】:

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

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

这应该适用于 TypeScript 2.1+。

related issue.

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 2019-08-25
    • 2020-12-16
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多