awslabs / aws-jwt-verify

JS library for verifying JWTs signed by Amazon Cognito, and any OIDC-compatible IDP that signs JWTs with RS256, RS384, and RS512
Apache License 2.0
606 stars 42 forks source link

When using type CognitoJwtVerifierSingleUserPool, getting compile error: "Types have separate declarations of a private property 'jwksCache'" #47

Closed jpbelang closed 2 years ago

jpbelang commented 2 years ago

Describe the bug I wanted to declare a type for a verifier like this:

import {CognitoJwtVerifier} from "aws-jwt-verify";
import {CognitoJwtVerifierSingleUserPool} from "aws-jwt-verify/cognito-verifier";

export type JWTConfig = {
    userPoolId: string,
    tokenUse: "access"|"id",
    clientId: string,
    graceSeconds?: number
}

const verifier: CognitoJwtVerifierSingleUserPool<JWTConfig> = CognitoJwtVerifier.create({
    userPoolId: "somepool",
    tokenUse: "id",
    clientId: "someclientid",
    graceSeconds: 10
})

But compilation returns an error:

src/verifier.ts:12:7 - error TS2322: Type 'CognitoJwtVerifierSingleUserPool<{ userPoolId: string; tokenUse: "id"; clientId: string; graceSeconds: number; }>' is not assignable to type 'CognitoJwtVerifierSingleUserPool<JWTConfig>'.
  Types have separate declarations of a private property 'jwksCache'.

12 const verifier: CognitoJwtVerifierSingleUserPool<JWTConfig> = CognitoJwtVerifier.create({
         ~~~~~~~~
Found 1 error.

Versions Which version of aws-jwt-verify are you using? 2.1.0

Which version of Node.js are you using? (Should be at least 14) 14.17.1

If using TypeScript, which version of TypeScript are you using? (Should be at least 4) Version 4.5.5

To Reproduce Just declaring a type seems to cause the problem. I have a repo for reproduction: https://github.com/jpbelang/aws-jwt-verify-check

I'm not an expert around this, but navigating through IntelliJ, CognitoJwtVerifier.create() seems to go in dist/cjs, where CognitoJwtVerifierSingleUserPool seems to come from dist/esm.

I can cheat by importing something that seems wrong: import {CognitoJwtVerifierSingleUserPool} from "aws-jwt-verify/dist/cjs/cognito-verifier";

jpbelang commented 2 years ago

As an aside, swc compiles the code correctly.

ottokruse commented 2 years ago

Thanks for taking the effort of reporting this issue and providing code to reproduce it!

I'm not an expert around this, but navigating through IntelliJ, CognitoJwtVerifier.create() seems to go in dist/cjs, where CognitoJwtVerifierSingleUserPool seems to come from dist/esm.

Yeah that is weird and seems to be the issue, we'll dive in this.

For the time being, this works too:

import {
  CognitoJwtVerifierSingleUserPool,
  CognitoJwtVerifier,
} from "aws-jwt-verify/cognito-verifier";
ottokruse commented 2 years ago

Fixed in v2.1.1