Reputation: 5213
I need to make a .d.ts
file for a npm module I have this npm module https://github.com/jeremyckahn/keydrown I am writing keydrown.d.ts
It has a weird interface:
import KD from 'keydrown';
KD.run(function KDRunTick() {KD.tick();});
//Forward
KD.W.press(() => dispatch({ type: "@INPUT/MOVE-FORWARD/PRESS" }));
KD.W.up(() => dispatch({ type: "@INPUT/MOVE-FORWARD/UP" }));
//Back
KD.S.press(() => dispatch({ type: "@INPUT/MOVE-BACK/PRESS" }));
KD.S.up(() => dispatch({ type: "@INPUT/MOVE-BACK/UP" }));
//Left
KD.A.press(() => dispatch({ type: "@INPUT/MOVE-LEFT/PRESS" }));
KD.A.up(() => dispatch({ type: "@INPUT/MOVE-LEFT/UP" }));
///etc
My current .d.ts
file looks like:
declare module 'keydrown' {
interface KeyObject{
press(callback: Function): void;
up(callback: Function): void;
}
export default interface KD {
run(callback: Function): void;
ZERO:KeyObject
ONE:KeyObject
TWO:KeyObject
THREE:KeyObject
FOUR:KeyObject
FIVE:KeyObject
SEVEN:KeyObject
EIGHT:KeyObject
NINE:KeyObject
A:KeyObject
B:KeyObject
C:KeyObject
D:KeyObject
E:KeyObject
F:KeyObject
G:KeyObject
H:KeyObject
I:KeyObject
J:KeyObject
K:KeyObject
L:KeyObject
M:KeyObject
N:KeyObject
O:KeyObject
P:KeyObject
Q:KeyObject
R:KeyObject
S:KeyObject
T:KeyObject
U:KeyObject
V:KeyObject
W:KeyObject
X:KeyObject
Y:KeyObject
Z:KeyObject
ENTER:KeyObject
SHIFT:KeyObject
ESC:KeyObject
SPACE:KeyObject
LEFT:KeyObject
UP:KeyObject
RIGHT:KeyObject
DOWN:KeyObject
BACKSPACE:KeyObject
DELETE:KeyObject
TAB:KeyObject
TILDE:KeyObject
CTRL:KeyObject
}
}
Unfortunately this throws an error in the implementation:
'KD' only refers to a type, but is being used as a value here. [2693]
Upvotes: 1
Views: 267
Reputation: 187252
A typescript module can export both values and types. So when you say export default interface
you are declaring a type that is exported.
Instead, you want to declare that the module exports a value. To do that add a const
to your module declaration and set that as the default export.
const defaultExport: KD
export default defaultExport
export interface KD { ... } // removed "default"
Upvotes: 2