Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading