-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
[@types/koa-router] incompatible type with koa #36161
Comments
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"strictPropertyInitialization": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"outDir": "dist",
"sourceMap": true
},
"include": [
"src"
]
} |
I also get this error, however you can use BaseContext instead and things will work, I don't know yet if this is expected, but it's not clear which Interface should be used in both koa and koa router contexts. |
The error says it's expecting In the Koa definition file I found this line @ejose19, (I'm completely new to TypeScript so I don't really know this stuff, just reading the errors and source code and trying to make sense of things.) |
Yes, Parametrized context has way more stuff than BaseContext, however it's still missing some important things like login / logIn / logout / logOut (which they are in Context alone) but if you try to use ctx: Context inside a koa-router route it says it's incompatible. Hope Anyone fix this because we should be using the correct interface. |
I digged a bit more and saw that libraries like koa-session / koa-generic-session / koa-passport (and probably many more) are creating / extending their own interface Context (here's an extract from @types/koa-passport)
That's why ParametrizedContext type is missing all those libraries types, my knowledge in typescript is not enough to solve this issue, but I hope this information leads to the proper resolution. |
For anyone still stuck on this issue, simply removing the type definition on the
|
If I use require I don't have an issue using a new Koa() koa-websocket: |
I found another way to fix this issue--blow away The problem is all the @types koa additives like When one updates the koa types to a new version, the old downloads for additive packages are not updated too. Deleting the node_modules and lock files causes npm or yarn to redownload everything to the same version of @types/koa. |
Remove The following code can be used to reproduce this issue with `koa-session':
When run this code with
|
Same with
|
same issue here |
Any news? I've the same problem. |
the problem seems to be 2.0.52. if forced to 2.0.50
and I also force to 2.0.50 in dependencies
it works again! |
same problem.and use |
Yes, we're having this same exact issue and have also force locked to |
How to solve? |
@hengkx if you first remove it, then do:; |
@lili21 we can resolve this with Intersection Types. import { RouterContext } from "koa-router";
router.get(
'/auth',
async (ctx: Context & RouterContext): Promise<void> => {
ctx.body = 'aaa'
}
) |
declare module "koa" {
interface Context {
session: session.Session | null;
readonly sessionOptions: session.opts | undefined;
}
} Fortunately or unfortunately import Router from 'koa-router';
import { Context } from 'koa';
const router = new Router();
router.post('/', (ctx: Context) => { // error: Argument of type '(ctx: Context) => void' is not assignable to parameter of type 'Middleware<ParameterizedContext<any, IRouterParamContext<any, {}>>>' because it is missing the following properties from type 'Context': session, sessionOptions
ctx.body = 'Hello worls';
}); To fix this problem we just need to use capabilities of generic Router class import Router from 'koa-router';
import { DefaultState, Context } from 'koa';
const router = new Router<DefaultState, Context>();
router.post('/', (ctx: Context) => {
ctx.body = 'Hello world';
}); Some people recommend to use You may not have such an issue if you use |
@zfeed Your solution worked extremely well, now all library methods (session, passport) are accessible inside ctx router middleware, also just declaring the interface once per router instead of on each route handles this issue very elegantly. Cheers! |
A note for users defining their route callback outside the route (like this) function getUser(ctx: Context){...}
router.post('/user/:id', getUser) In that case typescript won't know about the declaration merging and Context won't contain router specific properties (like ctx.params) One solution is to use a custom defined "RouterContext" that applies the type mentioned by @zfeed : export type RContext = ParameterizedContext<
DefaultState,
Context & Router.IRouterParamContext<DefaultState, Context>
>;
function getUser(ctx: RContext) {
ctx.params; // <--- this will be found
} And now both router properties and koa will be available as RContext |
@ejose19 Thank you very much. |
Thanks @zfeed |
Typescript sucks. |
Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the |
If you know how to fix the issue, make a pull request instead.
@types/koa-router
package and had problems.Definitions by:
inindex.d.ts
) so they can respond.Source Code
Error Message
The text was updated successfully, but these errors were encountered: