Search code examples
node.jstypescriptnode-modulesnewrelictypescript-typings

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

Solution

  • I forgot to export the functions itselfs. I made the newrelic Interface of course, but forgot to export the functions itselfs. The correct Declaration file should be:

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