@types/xxxx package and had problems.Definitions by: in index.d.ts) so they can respond.I upgraded @types/koa from 2.0.39 to 2.0.43. Now, lines like
this.app.use(favicon(__dirname + 'favicon.ico'));
give errors like
TS2345: Argument of type 'compose.Middleware<Context>' is not assignable to parameter of type 'compose.Middleware<Application.Context>'.
where favicon comes from import * as koaFavicon from 'koa-favicon'; and where this.app is of type Koa. This happens with several middlewares, it's not just an issue with koa-favicon. The problem seems to be due to changes in @types/koa but I'm not sure what the issue really is and how to fix it (i.e., has the syntax for using middlewares been changed, or do middlewares need to export themselves differently, or is this a bug with @types/koa?). Any ideas or suggestions are greatly appreciated!
It appear that type Middleware in Koa declaration file doesn't declare.
Same thing happened while making my PR to koa-csrf
It need to be fixed asap because making apps on Koa using Typescript is now almost impossible without errors about middlewares.
It does work when doing
import {Middleware} from "./node_modules/@types/koa/index";
function test(): Middleware {};
But doing
import * as Koa from "koa";
only import Context and Request
What version of typescript are you using, just curious? (I'm on 2.5.3.) I wonder if Typescript changes handling of default exports in 2.6.x, might be worth a try. Otherwise, sounds like the type alias needs to be explicitly exported somehow, to avoid having to do something like ^^ that?
2.6.2
Actually it was because of include on tsconfig.json lol
Your tsconfig.json broke things, or your tsconfig.json was what made your solution above work? I don鈥檛 think anything in my tsconfig.json could be causing this issue...
One of the folder included in include had a declaration file that was overriding the module koa without having import * as Koa from 'koa'; in the header.
Hmm, I only see 3 modules in my codebase where there's a line like declare module "koa", and in all cases that's preceded by import * as Koa from "koa";, so I don't think that's my issue unfortunately. Wonder what else could be going on...
I'm also running in to this issue with latest Node (v9.11.1) and latest TypeScript (v2.8.1). Did you manage to resolve it @DABH @haskaalo?
No, I just held the package back in my repo. I鈥檒l probaboy try upgrading again in the next few weeks.
FWIW with the latest versions, e.g. types/[email protected], types/[email protected], [email protected], everything seems to work again. Still no idea what the underlying issue was here, unfortunately, but I'm going to go ahead and close this since I'm no longer experiencing these problems. Guess some magical package updates have saved the day.
Still getting the same errors with types/[email protected]. Strange... my workaround for now is:
import * as Koa from "koa"
export const app: any = new Koa()
Running into this right now.
Current koa definitions seem to have created nasty conflicts with koa-body, koa-router, @ koa/cors, among others.
This is not resolved. [email protected], @types/[email protected]. koa-logger and koa-static both failing compilation with:
error TS2345: Argument of type 'compose.Middleware<Context>' is not assignable to parameter of type 'compose.Middleware<Context>'.
Types of parameters 'context' and 'context' are incompatible.
Type 'Context' is not assignable to type 'Context'. Two different types with this name exist, but they are unrelated.
Property 'params' is missing in type 'Context'.
@DABH please reopen
Looking farther, the dependencies of koa-static and koa-logger use @types/[email protected]. Setting my project typing to @types/[email protected] gets past the compiler.
Sure happy to reopen.
Why do those packages refer to an inconsistent version? Maybe the issue is that all your koa packages must refer to the same types versions? (I.e. the errors arise due to inconsistent versions?)
Interesting. koa-body depends on @types/[email protected] as well (though as a devDependency). I don't see koa-static having any dependency on @types/koa at all, though.
I too am experiencing this issue with @koa/cors, koa-bodyparser, koa-router, even when I revert types to @types/[email protected].
It doesn't look like they are referring to an inconsistent version in my code; just seems that the types for Context defined in these libraries vs those in Koa itself don't contain all the same entries. Messing around with the types right now to see what might resolve these errors.
I just nuked my node_modules and yarn.lock then reinstalled, and it looks like it's all good now. I think @DABH is right, perhaps if you incrementally add these libraries they get koa versions that actually aren't in sync, and then it fails. Not particularly intuitive, but this usability kink seems like partially an issue with how NPM deals with dependency versions, partially an issue with type compatibility in TypeScript.
I got the same however using tsc --skipLibCheck solved it for me.
Most helpful comment
It need to be fixed asap because making apps on Koa using Typescript is now almost impossible without errors about middlewares.