Closed Yrlish closed 3 years ago
And where are the type declarations?
They are automatically generated with the command mentioned above.
I've tried it, but that didn't work right, that's all it generated:
export namespace algorithms {
namespace HS256 {
const name: string;
namespace hash {
const name_1: string;
export { name_1 as name };
}
}
namespace HS512 {
const name_2: string;
export { name_2 as name };
export namespace hash_1 {
const name_3: string;
export { name_3 as name };
}
export { hash_1 as hash };
}
}
I'm not that experienced with TS, but I think I might have to go in and create the declarations by hand.
Hmm, it shouldn't be like that. I'm going to verify it again.
I did some changes to the configuration, in hope it will work better. For me it didn't change anything. This is what my index.d.ts
contains after running tsc
.
declare const _exports: JWT;
export = _exports;
declare class JWT {
algorithms: {
HS256: {
name: string;
hash: {
name: string;
};
};
HS512: {
name: string;
hash: {
name: string;
};
};
};
utf8ToUint8Array(str: any): Uint8Array;
sign(payload: any, secret: any, alg?: string): Promise<string>;
verify(token: any, secret: any, alg?: string): Promise<boolean>;
decode(token: any): any;
}
Ok, I took your output, updated it by hand to the right types, and got rid of the private methods. However now we got TS support. Thanks for your help :)
Great! Although, you should avoid using object
as a type. It is bad. Use any
instead, for simplicity. With any one can throw in classes. :)
ESLint: Don't use
object
as a type. Theobject
type is currently hard to use (see this issue). Consider usingRecord<string, unknown>
instead, as it allows you to more easily inspect and use the keys. (@typescript-eslint/ban-types)
But it's literally expecting it to be an object
here, see:
https://github.com/tsndr/cloudflare-worker-jwt/blob/eebc0e988a63dd1cfc7c5729e327b2cd219167f7/index.js#L33
Correct me if I'm wrong, but in this circumstance changing it to any
might be a bit confusing.
Oh yeah. That is true. My bad, I though I threw in a class, but it was an object.
await JWT.sign({...data, expires: expires}, SECRET)
In the case here, my variable data
is an instance of a interface, converted into an object with an extra entry. 🙂
Although, recommendation to avoid object
stands, as per ESLint recommendation. 🙂
You don't need to convert your interface to an object like this, the object
type accepts interfaces as well. I just tried it and no errors for me.
Yes, I know. It's just for adding more entries that is not part of the interface.
But you should import it like this:
import * as JWT from "@tsndr/cloudflare-worker-jwt"
Since I got an error otherwise.
Before publishing, the
tsc
command needs to be executed, or thedeclarations
script inpackage.json
. This will generate aindex.d.ts
file in the root folder of the repository.I've tested with manually copying the generated d.ts file into the correct node_modules folder in my project, at it seems to work nicely in my environment.