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, 2022 in TypeSript by Nina
• 3,060 points
585 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, 2022 in TypeSript by Nina
• 3,060 points
926 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, 2022 in TypeSript by Nina
• 3,060 points
2,737 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, 2022 in TypeSript by Nina
• 3,060 points
3,182 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, 2022 in TypeSript by Logan
• 2,140 points
542 views
0 votes
0 answers

Alternative to decorate function in TypeScript

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

Jul 14, 2022 in TypeSript by Logan
• 2,140 points
500 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, 2022 in TypeSript by Nina
• 3,060 points
1,111 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, 2022 in Others by narikkadan
• 63,600 points
2,237 views
0 votes
1 answer

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

Just add this in the app.module.ts file: import { FormsModule ...READ MORE

answered Apr 30, 2022 in Other DevOps Questions by narikkadan
• 63,600 points
4,490 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, 2022 in TypeSript by Nina
• 3,060 points
4,875 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP