#How do I pass a lambda as an argument
22 messages · Page 1 of 1 (latest)
It's just called function type.
type Callback = (arg: number) => string
function foo(callback: Callback) {
const result = callback(42)
// result is type string
}
foo(arg => `Hello ${arg}`)
right but there is no "Function" type
how do I do it without creating my own type
I just want an async void
You can just inline it.
how
function foo(callback: (arg: number) => string) {
// ...
}
If you want an async void, it's the same way as you would in C#.
uhm in C# I tend to use Action
type Func = () => Promise<void>
Action really is just C#'s workaround for inability to use void as a type, so it can't do Func<void>.
I see
Same thing for why C# has to have Task and Task<T>, where in TS there's just Promise<T> (and use Promise<void> if it doesn't return anything*)