Node.js TS:模块扩充<;类型>;仅引用类型,但在此处用作值
我试图在节点中手动创建()的类型,但遇到问题。当我创建以下定义时:Node.js TS:模块扩充<;类型>;仅引用类型,但在此处用作值,node.js,typescript,Node.js,Typescript,我试图在节点中手动创建()的类型,但遇到问题。当我创建以下定义时: // File: src/types/winston-syslog/index.d.ts declare module 'winston-syslog' { import * as Transport from 'winston-transport'; export interface SyslogTransportOptions extends Transport.TransportStreamOptions
// File: src/types/winston-syslog/index.d.ts
declare module 'winston-syslog' {
import * as Transport from 'winston-transport';
export interface SyslogTransportOptions extends Transport.TransportStreamOptions {
host?: string;
port?: number;
path?: string;
protocol?: string;
pid?: number;
facility?: string;
localhost?: string;
type?: string;
app_name?: string;
eol?: string;
levels?: {[key: string]: number};
}
export interface Syslog extends Transport {
new(options?: SyslogTransportOptions): Syslog;
}
}
我无法创建Syslog实例,因为Typescript失败并出现以下错误:
错误TS2693:“Syslog”仅引用类型,但在此处用作值
这是我的(大幅缩减的)主文件:
// File: src/Log.ts
import {Syslog, SyslogTransportOptions} from 'winston-syslog';
const transportOptions = {/* some values here */};
const syslogTransport = new Syslog(transportOptions);
我猜我如何定义Syslog类是有问题的,但这正是winston内部的做法。Syslog是“winston Syslog”包中的一个实例,而不是一个接口。您可以在单独的*.d.ts文件中编写稍有不同的类型定义
declare module "winston-syslog" {
export interface ISyslog {
new (options: any): ISyslog;
log(message: string): void;
}
export const Syslog: ISyslog;
}
然后你可以像这样使用打字:
import * as Test from "winston-syslog";
const t = new Test.Syslog({}); // The type is ISyslog
t.log("blabla");
谢谢,这很有帮助!我也偶然发现了。实现方式如下:
接口SyslogTransportInstance扩展传输{…}
然后导出const Syslog:SyslogTransportInstance代码>