javascripttypescriptcontract

What is the contract of a function or variable in TypeScript


I am a newbie at TypeScript and after installing it successfully on my machine, I came across the sentence "Type annotations in TypeScript are lightweight ways to record the intended contract of the function or variable" in TypeScript's official documentation page.

What is the word contract explained in the context of a function or variable in TypeScript.

The example that I took a look at was:

function greeter(person: string){
   return "Hello, " + person;
}

var user = "Jane User";

document.body.innerHTML=greeter(user);

Is it something to do with string annotation attached with the function variable person?

Any help on this is appreciated.

Here is the link to the official documentation


Solution

  • If you are interested in the word contract in this context: just as you can expect your employer to pay your salary at the end of each month according to your contract, you can expect that the argument of greeter will be a string (however you can break this contract very easily)