打字稿中的第三方模块的声明文件

我遇到了一些问题,把我刚刚为第三方软件包“ newrelic ”所做的声明文件整合起来。 总是当我运行tsc我得到了下一个错误信息:

 src/Server.ts(17,7): error TS2322: Type '{ express: typeof e; newrelic: typeof 'newrelic'; }' is not assignable to type 'BootServicesInterface'. 

财产types“newrelic”是不相容的。 键入'typeof'newrelic''不能分配键入'newrelic'。 属性'setTransactionName'在'typeof'newrelic''types中缺less。

有没有人如何解决这个错误? 我已经工作了几个小时,我看不出我做错了什么。 源文件:

./src/Server.ts

 'use strict' import * as debugDep from 'debug' const debug = debugDep('server') debug('Booting Server') debug('Loading .env file') import * as dotenv from 'dotenv' dotenv.config({silent: true}) debug('Loading System Dependencies') import * as express from 'express' import * as newrelic from 'newrelic' import {BootClass, BootServicesInterface} from './Core/Boot' debug('Setup Webserver') const Services: BootServicesInterface = { express, newrelic, } const boot = new BootClass(Services) 

./src/Core/Boot.ts

 'use strict' import * as express from 'express' import * as newrelic from 'newrelic' export interface BootClassInterface { setup(): express.Express } export interface BootServicesInterface { newrelic: newrelic.newrelic express(): express.Express, } export class BootClass implements BootClassInterface { private services: BootServicesInterface public constructor(services: BootServicesInterface) { this.services = services } } 

./@CustomTypes/newrelic/index.d.ts:

 declare module 'newrelic' { export interface newrelic { setTransactionName: (name: string) => void, setControllerName: (name: string, action?: {}) => void, createWebTransaction: (url: string, handler: Function) => void, createBackgroundTransaction(name: string, group: string | null | undefined, handler: Function): void, createBackgroundTransaction(name: string, handler: Function): void, endTransaction: () => void, createTracer: (name: string, callback: Function) => void, recordMetric: (name: string, value: number | {count: number, total: number, min: number, max: number, sumOfSquares: number}) => void, incrementMetric: (name: string, amount?: number) => void, recordCustomEvent: (eventType: string, attributes: {}) => void, addCustomParameter: (name: string, value: string | number) => void, addCustomParameters: (params : {}) => void, getBrowserTimingHeader: () => string, setIgnoreTransaction: (ignored: boolean) => void, noticeError: (error: Error, customParameters?: {}) => void, shutdown(options: Options, callback: Function): void, rules: Rules, addNamingRule: (pattern: Pattern[], name: string) => void, addIgnoringRule: (pattern: string[]) => void, } export interface Rules { name: Pattern[], ignore: string[], } export interface Pattern { pattern: string, name: string, terminate_chain?: boolean, replace_all?: boolean, precedence?: boolean } export interface Options{ collectPendingData: boolean, timeout: number } } 

./tsconfig.json

 { "compilerOptions": { "module": "es6", "moduleResolution": "node", "noImplicitAny": true, "noImplicitReturns": true, "noImplicitThis": true, "noUnusedLocals": true, "noUnusedParameters": true, "removeComments": false, "skipLibCheck": false, "sourceMap": false, "strictNullChecks": true, "target": "ES2016", "outDir": "./lib", "declaration": true, "diagnostics": true, "alwaysStrict": true }, "exclude": [ "node_modules", "public" ], "include": [ "**/*.d.ts", "./src/**/*.ts" ], "typeRoots": [ "@CustomTypes", "node_modules/@types" ], "lib": [ "es6" ] } 

我忘了导出函数本身。 当然我做了newrelic接口,但忘了导出函数本身。 正确的声明文件应该是:

./@CustomTypes/newrelic/index.d.ts:

 declare module 'newrelic' { export interface Rules { name: Pattern[], ignore: string[], } export interface Pattern { pattern: string, name: string, terminate_chain?: boolean, replace_all?: boolean, precedence?: boolean } export interface Options{ collectPendingData: boolean, timeout: number } export interface MetricValue{ count: number, total: number, min: number, max: number, sumOfSquares: number } export interface newrelic { setTransactionName(name: string): void setControllerName(name: string, action: {}): void setControllerName(name: string): void createWebTransaction(url: string, handler: Function): void createBackgroundTransaction(name: string, group: string| null, handler: Function): void createBackgroundTransaction(name: string, handler: Function): void endTransaction(): void createTracer(name: string, callback: Function): void recordMetric(name: string, value: number | MetricValue): void incrementMetric(name: string, amount?: number): void recordCustomEvent(eventType: string, attributes: {}): void addCustomParameter(name: string, value: string | number): void addCustomParameters(params : {}): void getBrowserTimingHeader(): string setIgnoreTransaction(ignored: boolean): void noticeError(error: Error, customParameters: {}): void noticeError(error: Error): void shutdown(options: Options, callback: Function): void rules: Rules addNamingRule(pattern: Pattern[], name: string): void addIgnoringRule(pattern: string[]): void } export function setTransactionName(name: string): void export function setControllerName(name: string, action: {}): void export function setControllerName(name: string): void export function createWebTransaction(url: string, handler: Function): void export function createBackgroundTransaction(name: string, group: string| null, handler: Function): void export function createBackgroundTransaction(name: string, handler: Function): void export function endTransaction(): void export function createTracer(name: string, callback: Function): void export function recordMetric(name: string, value: number | MetricValue): void export function incrementMetric(name: string, amount?: number): void export function recordCustomEvent(eventType: string, attributes: {}): void export function addCustomParameter(name: string, value: string | number): void export function addCustomParameters(params : {}): void export function getBrowserTimingHeader(): string export function setIgnoreTransaction(ignored: boolean): void export function noticeError(error: Error, customParameters: {}): void export function noticeError(error: Error): void export function shutdown(options: Options, callback: Function): void export var rules: Rules export function addNamingRule(pattern: Pattern[], name: string): void export function addIgnoringRule(pattern: string[]): void } 
Interesting Posts