#How can I assert the contents of a function in conjunction with `ReturnType<T>`?

12 messages · Page 1 of 1 (latest)

sweet spruce
#

One message removed from a suspended account.

idle trailBOT
#
giraffeunleashed#0

Preview:```ts
type F = {
k: <K>(arg: K) => K
}

type Test<O extends Record<any, any>> = {
[Key in keyof O]: O[Key] extends (
arg: number
) => number
? ReturnType<O[Key]>
: null
}

type Res = Test<F>```

coarse nimbus
#

The type of K isn't determined until you execute the function, so it can't really reason about the the types

sweet spruce
#

One message removed from a suspended account.

warped sandalBOT
#
u.mergeDocument(true)
coarse nimbus
#

If you specify the type on F then it can infer the types:


type F<K> = {
    k: (arg: K) => K
}

type Test<O extends Record<any, any>> = {
    [Key in keyof O]: O[Key] extends (arg: number) => number ? ReturnType<O[Key]> : null
}

type Res1 = Test<F<string>>
type Res2 = Test<F<number>>
sweet spruce
coarse nimbus
#

Correct, because the type belongs to the function, so it means the type is only known when the function is called

sweet spruce
#

One message removed from a suspended account.

warped sandalBOT
#
const entitites = createServicesFramework({
sweet spruce
#

One message removed from a suspended account.

#

One message removed from a suspended account.