为什么打字稿要投入键入错误:属性' customSelect'在类型上不存在,任何nock<

发布于 2025-01-20 19:55:48 字数 1760 浏览 0 评论 0原文

我收到错误:

typing error: Property 'customSelect' does not exist on type 'Knex<any, unknown[]>'

从函数

console.log({ inCreateConnection: ins.customSelect })

我在 typing.d.ts 中添加此接口以支持 customSelect,就像 knex 文档

// typings.d.ts
// from: https://knexjs.org/#typescript-support 
//       Extending Query Builder
import { Knex as KnexOriginal } from 'knex';

declare module 'knex' {
  namespace Knex {
    interface QueryBuilder {
      customSelect<TRecord, TResult>(
        value: number
      ): KnexOriginal.QueryBuilder<TRecord, TResult>;
    }
  }
}

打字在两个文件中都不起作用: https://github.com/wizardnet972/nx-knex/blob/main/libs/my-knex/src/lib/my-knex.ts#L10https://github.com/wizardnet972/nx-knex/blob/main/libs/product-api/src/lib/product-api.ts#L6

import knex from 'knex';

export const createConnection = () => {
  const ins = knex({
    connection: {
      host: 'localhost',
    },
  });

  console.log({ inCreateConnection: ins.customSelect }) /// <--- typing error: Property 'customSelect' does not exist on type 'Knex<any, unknown[]>'

  return ins;
};

这个问题是关于打字的,而不是应该运行。但打字应该可以工作。

为什么打字稿无法识别该功能?

I got an error:

typing error: Property 'customSelect' does not exist on type 'Knex<any, unknown[]>'

From the function:

console.log({ inCreateConnection: ins.customSelect })

I add this interface in typing.d.ts to support customSelect just as say in knex docs:

// typings.d.ts
// from: https://knexjs.org/#typescript-support 
//       Extending Query Builder
import { Knex as KnexOriginal } from 'knex';

declare module 'knex' {
  namespace Knex {
    interface QueryBuilder {
      customSelect<TRecord, TResult>(
        value: number
      ): KnexOriginal.QueryBuilder<TRecord, TResult>;
    }
  }
}

The typings isn't work in both files:
https://github.com/wizardnet972/nx-knex/blob/main/libs/my-knex/src/lib/my-knex.ts#L10 and https://github.com/wizardnet972/nx-knex/blob/main/libs/product-api/src/lib/product-api.ts#L6

import knex from 'knex';

export const createConnection = () => {
  const ins = knex({
    connection: {
      host: 'localhost',
    },
  });

  console.log({ inCreateConnection: ins.customSelect }) /// <--- typing error: Property 'customSelect' does not exist on type 'Knex<any, unknown[]>'

  return ins;
};

This problem is about typing and it's not supposed to run. but the typings are supposed to work.

Why typescript doesn't recognize the function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文