Monday, 24 April 2017

Declararion file for 3th party module in Typescript

I run into some problems with integrating a declaration file that i just made for the 3th party package "newrelic". Always when I run tsc I got the next error message:

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

Types of property 'newrelic' are incompatible. Type 'typeof 'newrelic'' is not assignable to type 'newrelic'. Property 'setTransactionName' is missing in type 'typeof 'newrelic''.

Does anyone how to fix this error? I worked already for several hours on it and I can't see what I'm doing wrong. The source files:

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



via Michel Bitter

No comments:

Post a Comment