Default type for Context and Handler prevents extension or override

Aperta
#583 3 commenti 6 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
node.js, typescript

Direzione di ricerca

Start by reading the Handler declaration in node_modules/openapi-backend/backend.d.ts and the registration example in the issue; reproduce the TypeScript error with the shown typed handler. Check how the default generic types interact with register and other handlers. Done means the intended handler can be registered without the reported type error, with the relevant type behavior covered by a test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

if i define a handler like so, and attempt to register it with other handlers, tsc will throw errors.

export const myHandler: Handler<
    unknown,
    { id: string }
> = async (
    c: OpenAPIBackendContext<unknown, { id: string }>,
    ctx: KoaContext,
): Promise<void> => {}
export const v1InternalAPI = new OpenAPIBackend({
    apiRoot: '/v1',
    definition: __dirname + '../../../api/service.v1.internal.yaml',
    // this is needed to honor the "format" field in the openapi definition
    customizeAjv: (originalAjv) => {
        addFormats(originalAjv);
        return originalAjv;
    },
});

// register your framework specific request handlers here
v1InternalAPI.register({
    postResponseHandler,
    validationFail,
    notFound,
    myHandler,
});

the errors

src/routes/v1/router.ts:27:5 - error TS2322: Type 'Handler<unknown, { id: string; }>' is not assignable to type 'Handler<any, UnknownParams, UnknownParams, UnknownParams, UnknownParams, Document>'.
  Property 'id' is missing in type 'UnknownParams' but required in type '{ id: string; }'.

27     myHandler,
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~

  src/controllers/myHandler.ts:12:7
    12     { id: string }
             ~~
    'id' is declared here.
  node_modules/openapi-backend/backend.d.ts:156:9
    156         [operationId: string]: Handler;
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from this index signature.


Found 1 error in src/routes/v1/router.ts:27

what am i doing wrong here?

Lingua principale
TypeScript
Stelle
689
Fork
97
Merge medio
2h 36m
PR unite (30g)
11

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openapistack/openapi-backend

Tutte le issue di openapistack/openapi-backend

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.