在 typescript 函数项目中添加 @google-cloud/logging 会出错

Adding @google-cloud/logging in typescript functions project gives error

编辑: 对于有同样问题的其他人 - 在 class 的主体中使用 const Logging: any = require('@google-cloud/logging')var logger = Logging() 就像一个魅力!

记得使用var logger = Logging()来实例化记录器库。否则你仍然会得到 logger.log is not a function!

原版post

我有一个 firebase 函数项目,用 typescript 编写 - 用 webpack 转译。我目前正在尝试实现 @google-cloud/logging 库,但我遇到了问题。它出来说

Could not find a declaration file for module "google-cloud/logging". Try npm install @types/@google-cloud/logging if it exists or add a new declaration (.d.ts) file containing declare module '@google-cloud/logging';

我尝试通过以下方式添加库:

import * as Logging from '@google-cloud/logging';
import * as logging from '@google-cloud/logging';
import { Logging } from '@google-cloud/logging';

但我仍然收到此错误。尝试 运行 使用 "logging" 的函数会导致

logging.log is not a function

我什至尝试了 javascript 请求库的方法,但仍然没有成功。

我的tsconfig.json:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "./",
        "noImplicitAny": true
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

我读到有些人通过手动将 "d.ts" 文件添加到项目中取得了成功,尽管我不太了解其中的内容。这是堆栈溢出中文章的 link -

我该如何在打字稿项目中添加库?

如果需要,我可以提供更多详细信息。这是我能想到的。

@google-cloud/logging 还没有类型定义。所以你需要提供一些! 与此同时,您可以

const Logging: any = require('@google-cloud/logging')

如果您安装了 @types/node 并以 nodejs 为目标 如果您以浏览器为目标但使用 "moduleResolution": "CommonJS"(您还需要提供节点类型)。

否则,您可以使用

import * as Logging from '@google-cloud/logging'

但在那种情况下,您需要声明此模块的类型

// logging.d.ts
declare module '@google-cloud/logging' {

  interface LogConfig {
    removeCircular: boolean
  }

  class Entry {
    metadata: object
    data: object
    constructor (metadata: object | null | undefined, data: object | string)
    constructor (data: object | string)
    toJSON (options?: LogConfig): any
  }

  interface WriteOptions {
    gaxOptions: object
    labels: object[]
    resource: object
  }

  type LogWriteCallback = (err: Error | null, apiResponse: object) => void
  type DeleteLogCallback = (err: Error | null, apiResponse: object) => void

  type LogWriteResponse = object[]
  type DeleteLogResponse = object[]

  type EntryArg = Entry | Entry[]

  class Log {
    constructor (logging: Logging, name: string, options: LogConfig)
    alert (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    critical (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    debug (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    emergency (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    info (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    notice (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    warning (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    error (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    write (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    delete (gaxOptions: object): Promise<DeleteLogResponse>
    delete (gaxOptions: object, callback?: DeleteLogCallback): Promise<DeleteLogResponse>
    delete (callback?: DeleteLogCallback): Promise<DeleteLogResponse>
  }

  interface ClientConfig {
    projectId?: string
    keyFilename?: string
    email?: string
    credentials?: {
      client_email: string
      private_key: string
    }
    autoRetry?: boolean
    maxRetries?: number
    promise?: Function
  }

  class Logging {
    constructor (options: ClientConfig)
    log (name: string, options?: LogConfig): Log
    entry (resource: object | string | null | undefined, data: object | string): Entry
  }
  export = Logging
}

这个定义只是一个草稿,缺少很多功能,但我想这是必要的第一步:-)