Your attempt was close, but you forgot to include the generic argument declaration in front of the arrow function parameters. Try something like this:
const log = <T>(message: T): IO<void> =>
new IO(() => console.log(message));
If you're working in a .tsx file, you may need to do something a little more complex to make it work.
Answer from JKillian on Stack OverflowYour attempt was close, but you forgot to include the generic argument declaration in front of the arrow function parameters. Try something like this:
const log = <T>(message: T): IO<void> =>
new IO(() => console.log(message));
If you're working in a .tsx file, you may need to do something a little more complex to make it work.
You could try to use:
const log = <T>(message: T): IO<void> => new IO(() => console.log(message));
typescript - Convert to Arrow Function - Stack Overflow
angular - Javascript Convert function to arrow function - Stack Overflow
typescript: How to write an arrow function with type declaration? - Stack Overflow
"Convert function declaration to expression or arrow function" (and vice versa)
Videos
You need no curly brackets, and no this for the console.
this.$scope.$watch(
() => this.$location.path(),
value => console.log(value)
);
There is no need to wrap location.path into another function. Also you could access console without this.
// no need to wrap single parameter like `(value)` for your tsLint config.
this.$scope.$watch(this.$location.path, value =>{
//this from parent
})
You can use map in typescript without any type like in JavaScript But
if you want to use type, you need to create an interface. Note that object does not have property name , email and other properties, so if you use object the ts compiler raise an error:
Try this one:
interface personlaInfo{
age: number,
gender: string,
address: string,
sport: string
}
interface user{
name:string,
email: string
personlaInfo:personlaInfo;
}
var users : Array<user> = [
{
name: "John",
email: "john@msn",
personlaInfo: {
age: 56,
gender: "Male",
address: "...",
sport: "basketball"
}
}
]
var f = users.map( (item:user) => (
{ name: item.name,
email: item.email,
...item.personlaInfo
}
));
PlaygroundLink
You don't need the second object, in this case TypeScript can infer it, like this:
users.map((item:object) => (
{ name: item.name,
email: item.email,
...item.user
}
));
When I was in the process of converting some legacy code to TS, I noticed a need to have a large set of functions with identical signature typed (it wasn't a very OOP design) that are not arrow functions. That raised the question of how to achieve the equivalent of:
type MyFunction = (a: string, b: SomeType) => Promise<boolean>;const f: MyFunction = (arg1, arg2) => { ... }
for a non-arrow function. Does this type of syntactical sugar just not exist...?
If you want to just convert your arrow functions to regular functions and keep the rest of the code compiling, you can set the target config to es5 and lib property to es6:
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
// ....
}
This will allow your code that uses ES6 features to compile while targeting ES5. But you have to make sure those features (like filter) are available at runtime. If they are not available it will throw runtime exceptions.
Arrow functions are a part of ES6, but you should be able to transpile your code to ES5 with Babel. There is a babel-preset-typescript which you should be able to use for this.