Default type for Context and Handler prevents extension or override

オープン
#583 コメント 3 件 リアクション 6 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
node.js, typescript

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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?

主要言語
TypeScript
スター
689
フォーク
97
平均マージ
2時間 36分
マージ済み PR(30日)
11

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openapistack/openapi-backend のほかの issue

openapistack/openapi-backend の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。