添加@ google-cloud / logging in typescript函数项目会产生错误

时间:2017-11-16 10:16:38

标签: typescript firebase google-cloud-functions google-cloud-logging

修改 对于有同样问题的其他人 - 在课堂上使用const Logging: any = require('@google-cloud/logging')var logger = Logging()一起工作就像一个魅力!

请记住使用var logger = Logging()来实例化记录器库。否则你仍然会得到 logger.log不是函数

原帖

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

  

找不到模块" google-cloud / logging"的声明文件。如果它存在,请尝试npm install @types/@google-cloud/logging或添加包含declare module '@google-cloud/logging';的新声明(.d.ts)文件

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

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

但我仍然收到此错误。试图运行一个使用" logging"结果

  

logging.log不是函数

我甚至尝试过要求图书馆的javascript方式,但仍然没有成功。

我的tsconfig.json:

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

我已经读到有些人通过添加" d.ts"手动归档到项目,虽然我不太了解它。以下是堆栈溢出文章的链接 - Importing non-typescript module in typescript

我怎样才能在打字稿项目中添加库?

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

1 个答案:

答案 0 :(得分:2)

@google-cloud/logging还没有类型定义。所以你需要提供一些! 在此期间,你可以做到

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

如果您安装@types/node并定位nodejs ,如果您定位浏览器但使用"moduleResolution": "CommonJS"(您还需要提供节点类型f)。

否则,您可以使用

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
}

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