#Why does `ReturnType<Schema<unknown>>` here return `any`?

1 messages · Page 1 of 1 (latest)

mild cave
#

If you scroll to the bottom of the code example, you'll see that Schema<T> is pretty well-defined as a function, but clearly I'm doing something wrong. It returns an object which should always have options and validator keys, but for some reason TS seems to be implying it should have type any?

cerulean apexBOT
#
advDanny#5480

Preview:```ts
type SuccessMultiple<T> = {
success: true
data: T[]
value: string[]
}

type SuccessRequiredSingle<T> = {
success: true
data: T
value: string
}

type SuccessOptionalSingle<T> = {
success: true
data: T | undefined
value: strin
...```

mild cave
#

I'm in the process of writing a library that lets users construct schemas for validating web forms, but everything leading up to Schema<T> should be defined to work in the example (save for Fetch which I've shemmed with type Fetch = unknown)

#

Ty for any help in advance 🙏

cerulean scarab
#

It looks like it's because the function has type parameters, so without those being specified to a certain value it doesn't know what it'll return so it just says it could be any value

#

Moving the Opts type arg onto Schema though instead of the function fixes it though for example:

cerulean apexBOT
#
Tetro#3432

Preview:```ts
type SuccessMultiple<T> = {
success: true
data: T[]
value: string[]
}

type SuccessRequiredSingle<T> = {
success: true
data: T
value: string
}

type SuccessOptionalSingle<T> = {
success: true
data: T | undefined
value: strin
...```

cerulean scarab
#

I don't know how your library will be set up so I don't know if that's a good solution or not but that looks like it's the cause of it at least

mild cave
#

Aaah thank you, greatly appreciated, yes that does fix it. I also just discovered this way works too. I'm guessing this fixes it for basically the same reason?

type SchemaResult<T, Opts extends Options<T>> = {
    options: Required<Options<T>>;
    validator: (
        values: string[],
        validatorOptions?: { fetch?: Fetch },
    ) => Promise<ValidatorResult<T, Opts>>;
};

type Schema<T> = <Opts extends Options<T>>(
    options?: Opts,
) => SchemaResult<T, Opts>;
cerulean scarab
#

The function on the otherhand will vary in ReturnType based on how someone would use it

mild cave
#

That makes sense, thank you again @cerulean scarab! 🙏