Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CurrentUserChecker type compatibility bug #1495

Open
TechQuery opened this issue Feb 16, 2025 · 9 comments
Open

fix: CurrentUserChecker type compatibility bug #1495

TechQuery opened this issue Feb 16, 2025 · 9 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@TechQuery
Copy link

Description

@attilaorosz CurrentUserChecker type is broken after upgrading routing-controllers from 0.10.4 to 0.11.0.

Minimal code-snippet showcasing the problem

import Koa, { ParameterizedContext } from 'koa';
import { useKoaServer } from 'routing-controllers';
import { JsonWebTokenError } from 'jsonwebtoken';

class User {
    name: string;
}

interface JWTAction {
    context?: ParameterizedContext<JsonWebTokenError | { user: User }>;
}

const app = new Koa();

useKoaServer(app, {
    controllers: [],
    currentUserChecker: ({ context: { state } }: JWTAction) =>
        state instanceof JsonWebTokenError
            ? (console.error(state), null)
            : state.user
});

app.listen();

Expected behavior

No type error.

Actual behavior

error TS2322: Type '({ context: { state } }: JWTAction) => User' is not assignable to type 'CurrentUserChecker'.
  Type 'User' is not assignable to type 'User | Promise<User>'.
    Type 'import("source/index").User' is not assignable to type 'User'.
      'User' could be instantiated with an arbitrary type which could be unrelated to 'import("source/index").User'.

17     currentUserChecker: ({ context: { state } }: JWTAction) =>
       ~~~~~~~~~~~~~~~~~~

  node_modules/routing-controllers/types/RoutingControllersOptions.d.ts:73:5
    73     currentUserChecker?: CurrentUserChecker;
           ~~~~~~~~~~~~~~~~~~
    The expected type comes from property 'currentUserChecker' which is declared here on type 'RoutingControllersOptions'       
@TechQuery TechQuery added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Feb 16, 2025
@attilaorosz
Copy link
Member

Could you provide your ts version?

@TechQuery
Copy link
Author

Could you provide your ts version?

5.7.3

@attilaorosz
Copy link
Member

Could you setup a minimal repro repo? My local version doesn’t even allow me to deconstruct the state property because the context property is optional in Action

@serolgames
Copy link

Hello, I have the issue too

@attilaorosz
Copy link
Member

@serolgames Please setup a minimal repro repo.

@sezanzeb
Copy link

@serolgames Please setup a minimal repro repo.

Here you go: https://github.com/sezanzeb/currentuserchecker-type-error

@attilaorosz
Copy link
Member

Reverted

@alcalyn
Copy link

alcalyn commented Mar 5, 2025

thanks, typings are fixed for me

@TechQuery
Copy link
Author

thanks, typings are fixed for me

@attilaorosz my error is fixed with [email protected] & [email protected].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

5 participants