Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the parenthesis mean when used in a typescript type in a key? [duplicate]

Tags:

typescript

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!!

like image 224
RuiSiang Avatar asked Sep 10 '25 12:09

RuiSiang


1 Answers

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

like image 73
jcalz Avatar answered Sep 13 '25 03:09

jcalz