You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plugin<{}>中花括号的作用及hapi-auth-bearer-token类型语句疑问

Understanding declare var BearerToken: Plugin<{}> in hapi-auth-bearer-token's Type Definitions

Let me break this down for you like I would in a Stack Overflow answer—no overly formal jargon, just clear, practical explanations:

First, let's unpack the full line: declare var BearerToken: Plugin<{}>

  • declare var is TypeScript's way of telling the compiler, "This variable exists in the global scope, even though you can’t see its actual code implementation here." Since .d.ts files only define types (not runtime logic), this line links the global BearerToken object exposed by the hapi-auth-bearer-token plugin to a TypeScript type.
  • BearerToken is the name of the plugin object you’ll use when registering this auth plugin with your Hapi server (for example, server.register(BearerToken)).
  • Plugin<{}> is the standardized type Hapi uses for all valid plugins. The <> syntax marks it as a generic type—and that’s exactly where the {} comes into play.

What’s the purpose of the {} in Plugin<{}>?

Hapi’s Plugin generic type accepts one key argument: the type of the configuration object the plugin expects when you register it. Here’s what that empty object means:

  • {} represents an empty object type in TypeScript. This tells the compiler that the hapi-auth-bearer-token plugin doesn’t require any configuration options to work. You can either pass an empty object when registering it (server.register({ plugin: BearerToken, options: {} })) or omit the options entirely if the plugin allows it.
  • For contrast, if a plugin required a secret string and a token validation function, the generic would look like Plugin<{ secret: string; validateToken: (token: string) => Promise<boolean> }>. This would enforce that you pass those exact properties when setting up the plugin.
  • Using <{}> is stricter than using <any> because it prevents you from accidentally passing extra, unused configuration properties that the plugin doesn’t recognize or support.

内容的提问来源于stack exchange,提问作者sunknudsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:38:18