peter-murray / node-hue-api

Node.js Library for interacting with the Philips Hue Bridge and Lights
Apache License 2.0
1.18k stars 145 forks source link

Typescript error when importing #166

Closed joppino closed 4 years ago

joppino commented 4 years ago

Using version 4.0.4, typescript 3.7.5, node 12.14.1. Importing like this:

import { v3 as hue } from 'node-hue-api'

Gets me the following typescript error: ../../node_modules/node-hue-api/lib/api/Api.d.ts(34,31): error TS2304: Cannot find name 'Cache'.

If I place the Cache class declaration before the import, it works. It's like it imports Api.d.ts before stateCache.d.ts, resulting in an error.

export = Api;

declare class Cache { constructor(data: any); data: any; _lights: {}; getLight(id: any): any; get modelid(): any; get apiversion(): any; }

declare class Api {

peter-murray commented 4 years ago

Thanks, I will take a look at this tomorrow. The definitions are generated off jsdoc using the typescript compiler, so not sure what has gone wrong there.

peter-murray commented 4 years ago

I have taken a look at the current 4.0.4 version and put together the following TypeScript code to exercise the TypeScript definitions:

import {v3 as hue} from 'node-hue-api';

const HueApi = hue.api;

HueApi.createLocal('redacted ip addresss').connect(null, null, null)
    .then(api => {
        return api.configuration.getUnauthenticatedConfig();
    })
    .then(config => {
        console.log(JSON.stringify(config, null, 2));
    })

It works without any issues, locally for me under Windows using node version 12.14.1, npm version 6.13.4 and typescript npm module version 3.7.5.

Can you possibly try the above code and see if it still errors for you? If so can you possibly provide a code snippit with an example that fails as reported so I can look deeper into this?

joppino commented 4 years ago

Here is a snippet of the build: it fails when using tsc. It's enough to just create an import like below:

import 'module-alias/register'; // for the path aliases import HueApp from '@root/hueapp' // my app "main" file import { Parser } from 'xml2js' import { BridgeInfo, HueIcon, BridgeAuth } from '@defs/bridge' // App imports import { v3 as hue } from 'node-hue-api'

[jop@kushan ~/Desktop/hueapp]$ tsc

node_modules/node-hue-api/lib/api/Api.d.ts:35:31 - error TS2304: Cannot find name 'Cache'.

35 getCachedState(): Promise | PromiseLike;


node_modules/node-hue-api/lib/api/Api.d.ts:35:52 - error TS2304: Cannot find name 'Cache'.

35     getCachedState(): Promise<Cache> | PromiseLike<Cache>;

Found 2 errors.

Hope this helps. I forgot to mention my tsconfig.json:

{ "compilerOptions": { "experimentalDecorators": true, "module": "commonjs", "lib": ["es2020" ], "target": "es2019", "strict": true, "removeComments": true, "preserveConstEnums": false, "sourceMap": true, "newLine": "LF", "outDir": "dist", "baseUrl": "src", "resolveJsonModule": true, "paths": { "@libs/": ["libs/"], "@defs/": ["defs/"], "@root/": ["./"], "@routes/": ["routes/"] } }, "include": ["src/*/"], "exclude": ["node_modules", "*/.spec.ts"] }

peter-murray commented 4 years ago

Can you please try pre-release version 4.0.5-0 or tag next, as I have made some changes to the JSDoc which looks like it fixed it locally for me using your tsconfig.json settings.

joppino commented 4 years ago

Yes, it works perfectly. thank you very much