0

Reverse engineering https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/hapi-auth-bearer-token/index.d.ts and can’t figure out the following line.

declare var BearerToken: Plugin<{}>;
sunknudsen
  • 6,356
  • 3
  • 39
  • 76

3 Answers3

1

this means BearerToken variable is of type plugin which holds objects inside

let fruits: Array<string> = ['Apple', 'Orange', 'Banana'];

  • Thanks Khashayar. So `Plugin<{}>` means `Plugin` is an array of objects? Is `Plugin<{}>` equivalent to `Plugin`? And what’s the difference with `Plugin[object]`? – sunknudsen Sep 19 '19 at 14:00
  • not actually Plugin is a type like Number or String you can declare new types in typescript script with `interface` like : `interface Plugin { // here is set of property with types which means the object with type Plugin must have these properties like : name: string }` so then ` Plugin<{}>` means our array contains objects of type Plugin and this is typescript so you see and read more about it in typescript website https://www.typescriptlang.org/docs/handbook/basic-types.html – Khashayar Pakkhesal Sep 20 '19 at 15:54
1

Plugin<{}> means Plugin is a generic type with a single type parameter, which is {} in this case.

{} is the empty object literal type (like { someProperty: SomeType }, but with no properties defined). It isn't the same as object, though:

const message: {} = 'hello world';

compiles (I am not sure why), but

const message: object = 'hello world';

doesn't.

(I also somehow can't find documentation for this form of types in TypeScript Handbook, but they are used in examples there, e.g. let { a, b }: { a: string, b: number } = o; or type Alias = { num: number }.)

EDIT: const message: { length: number } = 'hello world'; compiles (which makes sense) and { length: number } is a subtype of {}.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Very strange that `const message: {} = 'hello world';` compile. Sometimes I just can’t figure out TypeScript. Thanks for sharing your experiences. – sunknudsen Sep 19 '19 at 18:42
  • See https://stackoverflow.com/questions/49464634/difference-between-object-and-in-typescript – sunknudsen Sep 19 '19 at 19:01
  • And https://stackoverflow.com/questions/7675127/is-string-a-primitive-type-or-object-in-javascript – sunknudsen Sep 19 '19 at 19:05
  • Short story, in JavaScript, apparently a string is a primitive wrapped with an object (coerced) for fraction of time when certain operations are executed on it. By design, you can assign primitives to `{}`, but not to `object`. – sunknudsen Sep 19 '19 at 19:07
  • 1
    @sunknudsen The first question you linked is more relevant. See the edit as well. I'd like to see TypeScript better documented :( – Alexey Romanov Sep 19 '19 at 20:05
  • Thoughts on https://stackoverflow.com/questions/58018283/in-the-following-example-why-doesn-t-fleet-vehicle3-throw-an-error? – sunknudsen Sep 19 '19 at 20:10
  • Feel you Alexey, TypeScript is very quirky. The docs usually are there, somewhere. But the logic takes some getting used to. Love it though. – sunknudsen Sep 19 '19 at 20:14
  • Would love to see you expand your answer to include the fact that `{}` is a type assigned to a generic of the `Plugin` type definition. That’s what’s missing from both answers to make them useful to the next person who is trying to figure out these quirks. I might submit an educational answer. – sunknudsen Sep 19 '19 at 20:17
  • 1
    I thought you understood that part and that's why you only asked about the curly braces :) Edited. – Alexey Romanov Sep 19 '19 at 20:18
0

In general Plugin could be a generic class, generic interface or just array which only accepts objects. In the code Plugin comes from Hapi as per the import statement

import {
    Request,
    Plugin,
    ResponseToolkit,
    AuthenticationData,
  } from 'hapi';

I did a quick search and found the documentation for hapi plugins. I hope this helps