Home > Back-end >  What does the parenthesis mean when used in a typescript type in a key?
What does the parenthesis mean when used in a typescript type in a key?

Time:06-29

Can anyone please give an example object that fits the following typescript type? Kind of puzzled since I only used interfaces before and only saw squared brackets before.

type Hash = {
  (data: Uint8Array): Uint8Array
  blockLen: number
  outputLen: number
  create: any
}

Thanks!!

CodePudding user response:

It's a call signature. In this case, it means that a Hash, in addition to having blockLen, outputLen, and create properties, is also callable like a function that takes a parameter of type Uint8Array and returns a value of type Uint8Array:

const hash: Hash = function (x) { return x };
hash.blockLen = 1;
hash.outputLen = 2;
hash.create = "who knows";

const hashed = hash(new Uint8Array([1,2,3]));
// const hashed: Uint8Array

Playground link to code

  • Related