Typescript中第3方模块的声明文件

时间:2017-04-24 19:50:31

标签: node.js typescript node-modules newrelic typescript-typings

我遇到了一些问题,我正在为第三方软件包“newrelic”整合一个声明文件。总是当我运行tsc时,我收到了下一条错误消息:

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

属性'newrelic'的类型不兼容。     类型'typeof'newrelic''不能分配给'newrelic'类型。       类型'typeof'newrelic''中缺少属性'setTransactionName'。

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

./ 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 /核心/ 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"
    ]
}

1 个答案:

答案 0 :(得分:1)

我忘了导出功能本身。我当然创建了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
}