I'm working through a tutorial on TypeScript/ES6.
The following code works e.g. outputs ["Richard Francis Burton"]:
let convertToStringArray: (v: string) => string[] = (value) => [value];
let name = "Richard Francis Burton";
let names = convertToStringArray(name);
console.log(names);
But how does one understand the TypeScript/ES6 syntax?
convertToStringArrayis the function name with a parameter namevwhich has to be of typestring- this function returns
string[] = (value) => [value];
But this would be the same as the following function which doesn't work (Error TS1011: An element access expression should take an argument.)
function convertToStringArray(v: string) {
return string[] = (value) => [value];
}
(v: string) => string[]is the type (so a function which accepts a string, and returns an array of strings), whereas(value) => [value]is the function which is being assigned (which conforms to the type specified).string[]the return type? You can't return that, have you tried removing thestring[]after the return statement? You can put that after the parameter definitions for the functionconvertToStringArray(v: string): string[] {