【问题标题】:Typescript error: Property 'flat' does not exist on type '[string, unknown][]'打字稿错误:类型“[字符串,未知] []”上不存在属性“平面”
【发布时间】:2020-06-11 12:30:25
【问题描述】:

我需要在数组上使用 .flat(),但 Typescript 声称:

类型“[string, unknown][]”上不存在属性“flat”

我已将“es2019”添加到我的 tsconfig 文件中:

    "lib": ["es2015", "es2019", "dom"],

使用flat()的函数是:

const legendSeries = (series) => {
    const formattedSeries = {};
    series.map((serie) =>
      Object.entries(serie).map((s) => {
        if (s[0] in formattedSeries) {
          return (formattedSeries[s[0]].count += s[1]);
        }
        return (formattedSeries[s[0]] = { id: s[0], label: s[0], count: s[1] });
      })
    );
    return Object.entries(formattedSeries)
      .flat()
      .filter((_, i) => i % 2 !== 0);
  };

错误仍然存​​在。如何解决这个问题(这是一个 React 项目,以防它可能会有所帮助)?

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    我认为这个问题是因为 typescript 没有访问 array.flat() 的权限,在这种情况下,它会抛出一个不知道 flat() 的错误,正如 @codejockie 上面提到的。您需要更新配置文件,例如 { "compilerOptions": { "target": "es5", "lib": [ "es2019" ] } }

    我们需要将 es2019 或 es2019.array 添加到 TypeScript 的 --lib 设置,以便它开始识别 array.flat() 和 flatMap()。

    进行更改后。如果更改没有反映,请重新启动您的项目。

    【讨论】:

    • 对我来说关键是关闭 Visual Studio Code 然后重新启动它。在那之后,TypeScript 不再抱怨
    【解决方案2】:

    将您的 lib 更新为此 "lib": ["es2019", "es2017", "es7", "es6", "dom"]

    这是我的配置:

    {
      "compilerOptions": {
        /* Basic Options */
        "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
        "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
        "resolveJsonModule": true,
        "lib": [
          "es2019",
          "es2017",
          "es7",
          "es6",
          "dom"
        ] /* Specify library files to be included in the compilation. */,
        "declaration": true /* Generates corresponding '.d.ts' file. */,
        "outDir": "dist" /* Redirect output structure to the directory. */,
    
        /* Strict Type-Checking Options */
        "strict": true /* Enable all strict type-checking options. */,
        "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    
        /* Advanced Options */
        "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
        "baseUrl": ".",
        "paths": {
          "@/*": ["src/*"]
        }
      },
      "include": ["src/**/*.ts"],
      "exclude": ["node_modules", "dist", "examples", "**/*.spec.ts"]
    }
    

    【讨论】:

    • 问题仍然存在。打字稿一直在抱怨。
    • 我添加了我的配置供您参考
    • 我有 "lib": ["es2015", "es2017", "es2019", "es6", "es7", "dom"] 。 TS 返回错误是没有意义的。
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2018-11-30
    • 2017-03-02
    • 2021-09-07
    • 2021-10-23
    • 2017-11-25
    相关资源
    最近更新 更多