You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2017中调用[]keys()报错:类型'any[]'无'keys'属性原因咨询

Why You're Seeing "Property 'keys' does not exist on type 'any[]'" in TypeScript 2.8 + VS 2017

Let's break down exactly why this error is popping up for you:

  • ES6 Array Methods Aren't Included in Your Type Definitions
    TypeScript 2.8, when targeting ES5 (the default setup for many projects), doesn't automatically load type definitions for ES6+ array methods like keys(). The keys() method was added to arrays in ES2015, so if your compiler target is set to ES5, TypeScript's default type library only covers ES5-era features. Since it doesn't recognize keys() as a valid method on arrays, it throws that error.

  • Missing Library (lib) Configuration in tsconfig.json
    Even if you're targeting a newer ES version, if your tsconfig.json doesn't explicitly include the es2015 (or higher) library, TypeScript won't load the type definitions for ES2015 features. Without those definitions, it can't validate that keys() exists on an array.

  • Potential Syntax Misunderstanding
    It's also possible you're mixing up two similar operations:

    • Array.prototype.keys(): Returns an iterator for the array's indices (an ES6+ feature)
    • Object.keys(yourArray): Returns an array of string-based indices (works in ES5)
      If you intended to use the latter but accidentally called .keys() directly on the array, TypeScript will flag this if ES6 types aren't available.

Quick Checks to Confirm the Cause

To verify this is the root issue, you can:

  1. Open your tsconfig.json and set "target": "es2015" or higher
  2. Add "lib": ["es2015", "dom"] to your tsconfig.json to include ES2015 type definitions
  3. If you need to stay on ES5, swap yourArray.keys() with Object.keys(yourArray)

内容的提问来源于stack exchange,提问作者Serj-Tm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:03:46