#callback types

25 messages · Page 1 of 1 (latest)

dreamy ibexBOT
#
strobe#9977

Preview:```ts
type HandlerWithCtx<T extends Opts> = (
foo: string,
ctx: T["test"] extends true ? 1 : 0
) => Promise<void>

type Opts = {
test?: boolean
}

const wrap = <T extends Opts>(
cb: HandlerWithCtx<T>,
options: T
) => {
if (options?.test) {
return (foo: string) => cb(foo,
...```

faint iris
#

the types are showing up properly when the function is called, but errors on L15, L17

#

could someone explain to me what's going on

#

Argument of type '1' is not assignable to parameter of type 'T["test"] extends true ? 1 : 0'.(2345)

robust lava
#

conditionals don't work well in function signatures

faint iris
#

am i going to be able to narrow the callback return type from the options?

robust lava
#

probably not

#

consider if Opts was { test?: true }

#

if exactOptionalPropertyTypes was enabled, then test is just true there

#

so T["test"] extends true ? 1 : 0 would be 1

#

but you could pass an object with test missing, which would then go to the else

#

it just isn't a safe pattern

faint iris
#

is there a pattern that could work? the idea is essentially that i need the type to be properly narrowed in the callback rather than using some catch-all type

robust lava
#

why?

#

there's no way to narrow it directly at runtime

#

so it just isn't a pattern that works well in ts either

#

what are you attempting to do with this?

faint iris
#

use the options to apply some function calls which will narrow the type to a concrete one

#

it's an idea that was suggested. i've tried it before and didn't get it working well

#

i'm considering maybe overloads but i have a suspicion that's the wrong direction

robust lava
#

i think that could work.

#

conditionals in signatures do what overloads do, but worse

#

i don't think you need the generics, either.

faint iris
#

this is the sort of idea i'm thinking of

#

there's got to be a better way to do it though?