在typescript函数项目中添加@googlecloud/logging会出现错误



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

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

原始帖子

我有一个firebase函数项目,用typescript编写,并用webpack进行了转换。我目前正在尝试实现@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.log不是函数

我甚至尝试过javascript方式来要求库,但仍然没有成功。

我的tsconfig.json:

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

我读到有些人成功地手动向项目中添加了一个"d.ts"文件,尽管我对此并不了解

如何在打字脚本项目中添加库?

如果需要,我可以提供更多细节。这就是我所能想到的。

@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
}

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

相关内容

最新更新