LoginSignup
1
0

More than 1 year has passed since last update.

【Deno】OakのRouterContext<R, P, S>でエラーになる時の対処法

Posted at

【エラーメッセージ】
TS2707 [ERROR]: Generic type 'RouterContext<R, P, S>' requires between 1 and 3 type arguments.

Deno や TypeScript が日々進化していることが理由なのか、ネットで拾ったコードが大体このエラーになる。ジェネリクスの引数を渡さないまま使っていることが原因。

解決策✨

インポート文を修正して、RouterContextを上書きで定義する

修正前.ts
import { Application, Router, RouterContext } from "https://deno.land/x/oak/mod.ts";

修正後.ts
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import type { RouterContext as XContext } from "https://deno.land/x/oak/mod.ts";
type RouterContext = XContext<any, any, any>;

すると、それ以降のコードはそのまま使えるようになる。
any 型を使いたくない人はもう少し工夫が必要になるけど、とりあえず動くところまで。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0