#Why does `ReturnType<Schema<unknown>>` here return `any`?
1 messages · Page 1 of 1 (latest)
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
...```
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 🙏
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:
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
...```
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
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>;
Yeah because in SchemaResult you'll specify all the types it'll need to have it fill in everything
The function on the otherhand will vary in ReturnType based on how someone would use it
That makes sense, thank you again @cerulean scarab! 🙏