timoxley / keycode

Convert between keyboard keycodes and keynames and vice versa.
MIT License
455 stars 62 forks source link

Exported External Package Typings File #28

Closed cmj91uk closed 8 years ago

cmj91uk commented 8 years ago

Hi,

When importing the module via the ES6 import syntax using Visual Studio Code an error is displayed about the typings file.

"Exported external package typings file '/../node_modules/keycode/keycode.d.ts' is not a module. Please contact the package author to update the package definition"

It doesn't stop the compilation via Babel and just appears as an error within VSCode. However it would be nice to not have the error so that the handy Warnings and Errors panel can display 0 and 0 again.

Thanks in advance.

timoxley commented 8 years ago

@artfuldev any ideas?

artfuldev commented 8 years ago

Will look into this shortly

hodavidhara commented 8 years ago

I've also seen a few other errors using the packaged d.ts file. I see:

error TS1036: Statements are not allowed in ambient contexts.

referencing the d.ts file itself. And I see:

error TS2349: Cannot invoke an expression whose type lacks a call signature.

where I am calling it. To be clear. I import it with the following syntax:

import * as keycode from 'keycode';

and use it as documented. I am on typescript v1.8.10 and here is my tsconfig in case it helps:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react"
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

I believe the first error can be fixed by removing the semi-colons after the interface declarations, and I think the second can be fixed by changing export default keycode; to export = keycode;.

Resulting keycode.d.ts file should look like:

declare interface CodesMap {
  [key: string]: number;
}

declare interface InverseCodesMap {
  [key: number]: string;
}

declare interface Keycode {
  (event: Event): string;
  (keycode: number): string;
  (name: string): number;
  codes: CodesMap;
  aliases: CodesMap;
  names: InverseCodesMap;
}

declare var keycode: Keycode;

export = keycode;

I'm not sure this works for all build configurations, but this is what worked for my relatively simple set up. Sorry for the long comment!