【问题标题】:Extend lodash types (@types/lodash) with an interface of "first" method for my project为我的项目扩展 lodash 类型(@types/lodash),使用“first”方法的接口
【发布时间】:2020-12-07 21:00:51
【问题描述】:

问题: 我发现@types/lodash 方法首先没有涵盖输入的案例类型是两个(多个)类型化数组的联合: // 在我的文件中,我有 从'lodash'导入*作为_;

interface First {
  name: string;
}

interface Second {
  age: number;
}

function performSomeLogic(): First[] | Second[] {
  if (Math.random() > 0.5) return [{name: 'Alice'}, {name: 'Bob'}];
  return [{age: 42}, {age: 33}];
}

// we got some array First or Second of type. ok
const returnedData = performSomeLogic(); // we got some array First or Second of type. ok

// we expect value is First or Second of type. NOT ok.

// *******************
// TS ERROR
// TS2345: Argument of type 'First[] | Second[]' is not assignable to 
// parameter of type 'ArrayLike<First>'.
// *******************
const firstValueOfData = _.first(returnedData); 

// further steps could be ..
const proceedWithValueOfData = (value: First | Second) => Object.keys(value);
proceedWithValueOfData(firstValueOfData);

想法: 我想出了 _.first() 的正确接口。我尝试将其放入 typings.d.ts 添加参考它以供捆绑器使用:

// typings.d.ts
type DeriveArrTypes<T> = T extends (infer R)[] ? R : T;

// So DeriveArrTypes<First[] | Second[]> returns First|Second union

declare module 'lodash' {
  // LoDashStatic IS THE INTERFACE LODASH HAS HAD. FIRST IS DECLARED IN IT. I WANT TO ADD ITS EXTENDED DECLARATION
  interface LoDashStatic {
    first<T extends []>(value: T): DeriveArrTypes<T> | undefined;
  }
}

成功 - 否: 但这不起作用。 Typescript 停止显示任何错误(包括其他不同的错误)。我使用 WebStorm 作为 IDE,它会继续突出显示。

环境: 它是一个前端项目,由 Typescript 用于转译和 Webpack 用于捆绑(由于其长期持续的活跃性:))。 来自 webpack.config.js

       // the rule for ts
       {
            test: /\.ts$/,
            use: [{
                loader: 'ts-loader',
                options: {
                    // disable type checker - we will use it in fork plugin
                    transpileOnly: true
                }
            }],
        },

       // plugins section
       plugins: [
         // bla-bla plugins
         ...

        new ForkTsCheckerWebpackPlugin({
          tsconfig: __dirname + '/tsconfig.json'
        })
      ]

来自 tsconfig.json

"compilerOptions": {
      // bla-bla options
      ... 

      "typeRoots": [
        "node_modules/@types"
      ],
 }

 // I ADDED THIS
 "include": [
    "app/typings/**/*"
  ],

当我创建 app/typings 目录并将 typings.d.ts 放入其中时。

【问题讨论】:

  • declare module "lodash" 是你想要的。这是您要扩充的模块说明符,因此是一个字符串
  • @AluanHaddad 非常感谢。我听从你的建议,因为它是正确的符号,我错过了。但是,它不能解决问题。我会说我看不到任何影响。
  • 这是一个先决条件。我不是说这就是你所要做的,我只是看了你的问题,但这是必要的。
  • @AluanHaddad 谢谢。我很感激。我在上面的问题中更新了我的语法。
  • 请注意,您需要在模块上下文中才能使其工作,否则它不是增强。只需将 export {} 添加到文件顶部,以确保即使您删除导入,它仍然是一个模块

标签: typescript lodash typescript-typings definitelytyped


【解决方案1】:

如果你想给一个 npm 模块添加接口,你可以使用 declare module 关键字

import lodash  from "lodash";
declare module "lodash " {
    interface First {
       name: string;
    }

    interface Second {
      age: number;
    }
}

【讨论】:

  • 它抛出一个错误This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag. 所以我添加为import * as lodash from 'lodash'; 现在它说TS2300: Duplicate identifier 'first'. 并指的是@types/lodash 中实现了同名接口的地方。
  • 您可以通过将"allowSyntheticDefaultImports": true,"esModuleInterop": true, 更改为您的 tsconfig.json 来修复它@AlekseyLysenko 我刚刚运行它,它工作得很好gist.github.com/golkhandani/f11cfeefd753e90aca0e3bb7facd8b17
  • 非常感谢您的帮助。使用您提到的标志,它不会在 typings 文件中引发错误。我的关键点是,我想扩展现有的 lodash 类型 LoDashStatic 接口。有 first 声明,但它的声明是不够的。如果有一种方法可以添加 lodash 可以使用的新界面,我会非常乐意使用它。
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 2017-01-10
  • 1970-01-01
相关资源
最近更新 更多