{"id":461,"date":"2022-11-01T14:58:38","date_gmt":"2022-11-01T06:58:38","guid":{"rendered":"https:\/\/islandtropicaman.com\/wp\/?p=461"},"modified":"2022-11-01T15:02:09","modified_gmt":"2022-11-01T07:02:09","slug":"create-a-callback-function-in-typescript","status":"publish","type":"post","link":"https:\/\/islandtropicaman.com\/wp\/2022\/11\/01\/create-a-callback-function-in-typescript\/","title":{"rendered":"Create a callback function in TypeScript"},"content":{"rendered":"<p>In this typescript example, I am going to create a callback function that will return a string depending on the text the user has entered into the second parameter of the calling function.<\/p>\n<p>First of all, let us create the callback function as follows:-<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction hello(text : string) : string {\r\n    if (text == &quot;Hello World!&quot;) {\r\n        return &quot;Hello Again World!&quot;\r\n    } else {\r\n        return &quot;Good Morning World!&quot;\r\n    }\r\n}\r\n<\/pre>\n<p>As you can see above that the call-back function will receive a string and will return another string based on the text that the user has entered.<\/p>\n<p>Next, let us create the call function which will receive two parameters, the first one is the callback function and the second parameter is the text which will then become the parameter of the call-back function.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction calltheworld(callbackfunc:(text:string)=&gt;string, message:string) {\r\n    console.log(callbackfunc(message))\r\n}\r\n<\/pre>\n<p>I am using the () => syntax as the function signature for the callback function which will receive one parameter of type string and return a string at the end.<\/p>\n<p>Now let us enter the parameters into the calling function as follows:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ncalltheworld(hello, &quot;hey&quot;)\r\n<\/pre>\n<p>The above line will return the following string:-<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nGood Morning World!\r\n<\/pre>\n<p>Next, let us enter another set of parameters into the above calling function again:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ncalltheworld(hello, &quot;Hello World!&quot;)\r\n<\/pre>\n<p>This time I have entered the &#8220;Hello World!&#8221; text which will make the callback function returns the below outcome.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nHello Again World!\r\n<\/pre>\n<p>At last, if I entered the following parameters into the callback function then the program will not compile due to the safeguarding protocal which will activate because the first parameter of the calling function which is a string will not match the signature of the first parameter which is a callback function which accepts a string as its parameter and returned another string at the end.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ncalltheworld(&quot;hey&quot;, &quot;he&quot;)\r\n<\/pre>\n<p>The parameter with a signature is a strong type parameter that will not allow the user to misenter any other parameter than the one specified by its parameter signature.   <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this typescript example, I am going to create a callback function that will return a string depending on the text the user has entered into the second parameter of the calling function. First of all, let us create the callback function as follows:- function hello(text : string) : string { if (text == &quot;Hello&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"https:\/\/islandtropicaman.com\/wp\/2022\/11\/01\/create-a-callback-function-in-typescript\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;Create a callback function in TypeScript&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[136,40],"class_list":["post-461","post","type-post","status-publish","format-standard","hentry","category-type-script","tag-call-back-function","tag-typescript"],"blog_post_layout_featured_media_urls":{"thumbnail":"","full":""},"categories_names":{"38":{"name":"Type-Script","link":"https:\/\/islandtropicaman.com\/wp\/category\/type-script\/"}},"tags_names":{"136":{"name":"call back function","link":"https:\/\/islandtropicaman.com\/wp\/tag\/call-back-function\/"},"40":{"name":"typescript","link":"https:\/\/islandtropicaman.com\/wp\/tag\/typescript\/"}},"comments_number":"0","_links":{"self":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/comments?post=461"}],"version-history":[{"count":3,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":464,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/461\/revisions\/464"}],"wp:attachment":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/media?parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/categories?post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/tags?post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}