#How do I pass a lambda as an argument

22 messages · Page 1 of 1 (latest)

digital raft
#

I want to pass a lambda as an argument to a (svelte) component and a function, what is the type called?

wet crag
#

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}`)
digital raft
#

how do I do it without creating my own type

#

I just want an async void

wet crag
#

You can just inline it.

digital raft
#

how

wet crag
#
function foo(callback: (arg: number) => string) {
    // ...
}
digital raft
#

ah right

#

thanks

#

and how do I do a void?

wet crag
#

If you want an async void, it's the same way as you would in C#.

digital raft
#

uhm in C# I tend to use Action

wet crag
#
type Func = () => Promise<void>
digital raft
#

right

#

thanks

wet crag
#

Action really is just C#'s workaround for inability to use void as a type, so it can't do Func<void>.

digital raft
#

I see

wet crag
#

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

digital raft
#

yeah got it

#

!solved