-1

All:

[UPDATE] 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?

Thanks

6
  • I'm actually not even sure that's valid TypeScript...? It doesn't seem to work when I paste it here: typescriptlang.org/Playground Commented Dec 2, 2015 at 21:41
  • @Katana314 Thanks, neither on my side, so basically it could be a typo? Commented Dec 2, 2015 at 21:42
  • Could this be the intention? var sayHello: (input: string) => string = function (s: string) { return "Hello " + s; }. It would be good to get confirmation before delving into explanation Commented Dec 2, 2015 at 21:45
  • @Katana314 not sure, but I guess so. This is from a book called "Typescript Revealed"(Pub. 2013), it uses this example to show why we need interface Commented Dec 2, 2015 at 21:47
  • If the book you're reading keeps having syntax errors in it, you should find a different book...? Commented Dec 2, 2015 at 21:50

1 Answer 1

1

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) {

Should be:

var sayHello: (input: string) => string = function (s: string) {

We are saying that sayHello is a function that takes a string and returns a string : (input: string) => string . Then we assign it to such a function = function (s: string) {

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I am reading your suggested gitbook. One thing gets me confused is the definition of interface, I wonder how to define an interface with a string member and a function member with one string parameter and return string?
I read from this post: blogs.msdn.com/b/typescript/archive/2013/01/24/… when specify a function inside a interface, I am not sure why it can use colon this time? (in Basic section: it use interface Greetable { greet(message: string): void; } )
@Kuan that difference is covered here : stackoverflow.com/questions/32043487/… Its just a property shorthand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.