nirvair
nirvair

Reputation: 4180

JavaScript to TypeScript type/interface mapping

I'm using this library in a TypeScript project. And this is how my class looks like:

import OnvifManager from 'onvif-nvt'
Class OnvifApi {
// device: any = undefined
device = {} as OnvifDevice
constructor (...params) {
// definition
}
connect (): Promise<any> {
  OnvifManager.connect(...params).then((response: OnvifDevice) => {
    this.device = response
    resolve(response)
  }
}
coreService(): Promise<Type[]> {
  this.device.add('core')
}
}

And this this interface I created for the response from onvif-nvt

interface OnvifDevice {
  address: string
  // type
  // type
  // type
  add(name: string): void
}

This class is always giving an error in coreService saying that

this.device.add is not a function.

EDIT: This is the return of the connect method, which returns the whole Camera object.

Things are working when device is defined to any.

How can I do this interface mapping from JavaScript to TypeScript?

Response from connect

Upvotes: 1

Views: 76

Answers (1)

user16790253
user16790253

Reputation: 128

The reason is, in the interface, OnvifDevice, add is a mandatory parameter, but the device object is initialised with an empty object.

The dynamic mapping at this.device = response is not happening. Also please check if the response has add method or not while you debug.

These are the two possible symptoms I see.

Upvotes: 2

Related Questions