Function definition syntax confuse in Typescript

0 votes

I figure that out: One post in SO explain this: In TypeScript how do I declare an array of functions that accept a string and return a string?
it turns out:

A function type literal of the form

( ParamList ) => ReturnType

is exactly equivalent to the object type literal

{ ( ParamList ) : ReturnType }

So, Colon: with Bracket{} <=> Arrow =>


I am pretty new to Typescript, there is one example confuses me with its syntax:

var sayHello: (input: string) : string = function (s: string) {
    return "Hello " + s;
}
var stringUtils: { (input: string): string; }[];
stringUtils.push(sayHello);

Could anyone help to give a little explanation what this code do? Especially what the first part definition does?

var sayHello: (input: string) : string = function (s: string) {
    return "Hello " + s;
}

The example says sayHello is a function variable, but when I run it, I got "=>" expected error, does this mean this syntax belong to old typescript but not exist now?

Jul 5 in TypeSript by Nina
• 3,020 points
14 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer

Destructure a function parameter in Typescript

You can use the object de-structuring syntax: createUser(parent: ...READ MORE

answered Jun 9 in TypeSript by Nina
• 3,020 points
16 views
0 votes
1 answer

How do I extend a TypeScript class definition in a separate definition file?

If you don't have control over the ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
132 views
0 votes
1 answer

What do square brackets mean in a type definition in Typescript?

I've missed a basic type of TypeScript: Tuples. So ...READ MORE

answered Jun 22 in TypeSript by Nina
• 3,020 points
65 views
0 votes
0 answers

In typescript, why do the | and & operators flip their meaning when used on function types?

In this code, example1 and example2 are confusing me: type F1 = ...READ MORE

Jul 13 in TypeSript by Logan
• 2,140 points
21 views
0 votes
0 answers

Alternative to decorate function in TypeScript

Since TypeScript allows decorate only class, method, ...READ MORE

Jul 14 in TypeSript by Logan
• 2,140 points
9 views
0 votes
1 answer
0 votes
1 answer

What does the @ (at sign) sign do in TypeScript?

The big news this week is the ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
20 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
689 views
0 votes
0 answers

Can't bind to 'ngModel' since it isn't a known property of 'input'

I have this  input in my component ...READ MORE

Apr 29 in Other DevOps Questions by Kichu
• 19,040 points
140 views
0 votes
1 answer

VS Code enforces semicolons, but why doesn't Angular and TypeScript not use it consistently?

JavaScript has a syntactic feature known as semicolon ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
44 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP