koa-respond icon indicating copy to clipboard operation
koa-respond copied to clipboard

Publish TypeScript definitions

Open G-Rath opened this issue 4 years ago • 7 comments

I'd love to use this in TypeScript, but there are no definitions 😬

I'm happy to contribute them, but I'm pretty busy right now, so I'm making an issue in the meantime incase someone's already made some, or wants to have a whack :)

I'll be publishing them to @types, unless you'd like them here.

G-Rath avatar Sep 29 '19 05:09 G-Rath

@G-Rath I created a repo that is inspired by koa-respond, it built with typescript. Typing hint is out of the box, so you enjoy it without installing any @types dependencies. image

jeremy-ww avatar Jan 10 '20 09:01 jeremy-ww

import * as Koa from 'koa';

declare module 'koa' {
    interface ExtendableContext {
        ok: (response?: string|object) =>  Koa.Context;
        created: (response?: string|object) =>  Koa.Context;
        noContent: (response?: string|object) =>  Koa.Context;
        badRequest: (response?: string|object) =>  Koa.Context;
        unauthorized: (response?: string|object) =>  Koa.Context;
        forbidden: (response?: string|object) =>  Koa.Context;
        notFound: (response?: string|object) =>  Koa.Context;
        locked: (response?: string|object) =>  Koa.Context;
        internalServerError: (response?: string|object) =>  Koa.Context;
        notImplemented: (response?: string|object) => Koa.Context;
    }
}

Simple declaration that gets you past the compiler (I don't know much about creating types for other repos)

ignaciojcano avatar Jan 28 '20 17:01 ignaciojcano

@G-Rath are you still planning to publish the types?

f4bD3v avatar Apr 07 '20 19:04 f4bD3v

Sorry, I missed this issue.

I didn't originally do this in TS because specifying custom context in Koa is already a mess as you would have to create your own context type that is the union of the contexts of all the libs you use.

Feel free to publish the typings on DT though.

jeffijoe avatar Apr 07 '20 19:04 jeffijoe

Hey @ignaciojcano where in your project did you put the module declaration?

tevonsb avatar Apr 16 '20 22:04 tevonsb

Hey @ignaciojcano where in your project did you put the module declaration?

@tevonsb created a types/koa-respond/index.d.ts but the important part is to tell the TS compiler where your types are, so also add "typeRoots": ["./types", "./node_modules/@types"] to your compilerOptions in your tsconfig. (notice that i only added ./types folder

ignaciojcano avatar Apr 17 '20 13:04 ignaciojcano

Thanks @ignaciojcano this helped us.

tevonsb avatar Apr 29 '20 23:04 tevonsb