Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,19 @@ function isPromise(v: any): v is Promise<any> {
*
* @description https://docs.solidjs.com/reference/basic-reactivity/create-resource
*/
export function createResource<T, R = unknown>(
fetcher: ResourceFetcher<true, T, R>,
options: InitializedResourceOptions<NoInfer<T>, true>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the problem be resolved by just removing the NoInfer of the T in the InitializedResourceOptions here? In that way, what goes on inside there can contribute to the type T. This would be cleaner than the parallel I mechanism if it works.

): InitializedResourceReturn<T, R>;
export function createResource<T, R = unknown, I = T>(
fetcher: (k: true, info: ResourceFetcherInfo<T | I, R>) => T | Promise<T>,
options: ResourceOptions<T | I, true> & { initialValue: I }
): InitializedResourceReturn<T | I, R>;
export function createResource<T, R = unknown>(
fetcher: ResourceFetcher<true, T, R>,
options?: ResourceOptions<NoInfer<T>, true>
): ResourceReturn<T, R>;
export function createResource<T, S, R = unknown>(
export function createResource<T, S, R = unknown, I = T>(
source: ResourceSource<S>,
fetcher: ResourceFetcher<S, T, R>,
options: InitializedResourceOptions<NoInfer<T>, S>
): InitializedResourceReturn<T, R>;
fetcher: (k: S, info: ResourceFetcherInfo<T | I, R>) => T | Promise<T>,
options: ResourceOptions<T | I, S> & { initialValue: I }
): InitializedResourceReturn<T | I, R>;
export function createResource<T, S, R = unknown>(
source: ResourceSource<S>,
fetcher: ResourceFetcher<S, T, R>,
Expand Down
30 changes: 30 additions & 0 deletions packages/solid/test/resource.type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ type Equals<X, Y> =
type Tests = Assert<Equals<ResourceActions["mutate"], Setter<number>>>;
}

// without source
// with fetcher, nullable initialValue
{
const resourceReturn = createResource(
() => {
return Promise.resolve(1);
},
{
initialValue: null
}
);

type Tests = Assert<Equals<typeof resourceReturn, InitializedResourceReturn<number | null>>>;
}

// without initialValue
// with source, fetcher
{
Expand Down Expand Up @@ -105,6 +120,21 @@ type Equals<X, Y> =
);
}

// with source, fetcher, nullable initialValue
{
const resourceReturn = createResource(
() => 1,
() => {
return Promise.resolve(1);
},
{
initialValue: null
}
);

type Tests = Assert<Equals<typeof resourceReturn, InitializedResourceReturn<number | null>>>;
}

/* Resource type tests */
{
let resource!: Resource<string>;
Expand Down
Loading