deno.land / x / solid@v1.5.6 / test / resource.type-tests.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import { createResource, ResourceReturn, createSignal, Resource, Setter} from "../src";import { InitializedResource, InitializedResourceReturn } from "../src/reactive/signal";
type Assert<T extends true> = T;// https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
/* createResource inference tests */
// without source, initialValue// with fetcher{ const resourceReturn = createResource( k => { return Promise.resolve(1); type Tests = Assert<Equals<typeof resourceReturn, ResourceReturn<number, unknown>>> & Assert<Equals<typeof k, true>>; }, { storage: createSignal, name: "test", deferStream: true, onHydrated: (k, info) => { type Tests = Assert<Equals<typeof k, true | undefined>> & Assert<Equals<typeof info, { value: number | undefined }>>; } } );}
// without source// with fetcher, initialValue{ const resourceReturn = createResource( k => { return Promise.resolve(1); type Tests = Assert< Equals<typeof resourceReturn, InitializedResourceReturn<number, unknown>> > & Assert<Equals<typeof k, true>>; }, { initialValue: 1, storage: createSignal, name: "test", deferStream: true, onHydrated: (k, info) => { type Tests = Assert<Equals<typeof k, true | undefined>> & Assert<Equals<typeof info, { value: number | undefined }>>; } } );
type ResourceActions = typeof resourceReturn[1]; type Tests = Assert<Equals<ResourceActions['mutate'], Setter<number>>>;}
// without initialValue// with source, fetcher{ const resourceReturn = createResource( () => 1, k => { return Promise.resolve(1); type Tests = Assert<Equals<typeof resourceReturn, ResourceReturn<number, unknown>>> & Assert<Equals<typeof k, number>>; }, { storage: createSignal, name: "test", deferStream: true, onHydrated: (k, info) => { type Tests = Assert<Equals<typeof k, number | undefined>> & Assert<Equals<typeof info, { value: number | undefined }>>; } } );
type ResourceActions = typeof resourceReturn[1]; type Tests = Assert<Equals<ResourceActions['mutate'], Setter<number | undefined>>>;}
// with source, fetcher, initialValue{ const resourceReturn = createResource( () => 1, k => { return Promise.resolve(1); type Tests = Assert< Equals<typeof resourceReturn, InitializedResourceReturn<number, unknown>> > & Assert<Equals<typeof k, number>>; }, { initialValue: 1, storage: createSignal, name: "test", deferStream: true, onHydrated: (k, info) => { type Tests = Assert<Equals<typeof k, number | undefined>> & Assert<Equals<typeof info, { value: number | undefined }>>; } } );}
/* Resource type tests */{ let resource!: Resource<string>; const resourceValue = resource(); let initializedResource!: InitializedResource<string>; const initializedResourceValue = initializedResource();
type Tests = Assert<Equals<typeof resourceValue, string | undefined>> & Assert<Equals<typeof initializedResourceValue, string>> & Assert< Equals<typeof resource.state, "errored" | "pending" | "ready" | "refreshing" | "unresolved"> > & Assert<Equals<typeof initializedResource.state, "errored" | "ready" | "refreshing">>;
switch (resource.state) { case "errored": const errorValue = resource(); break; case "pending": const pendingValue = resource(); break; case "ready": const readyValue = resource(); break; case "refreshing": const refreshingValue = resource(); break; case "unresolved": const unresolvedValue = resource(); break; // this is weird but it works type Test = Assert<Equals<typeof errorValue, never>> & Assert<Equals<typeof pendingValue, undefined>> & Assert<Equals<typeof readyValue, string>> & Assert<Equals<typeof refreshingValue, string>> & Assert<Equals<typeof unresolvedValue, undefined>>; }
switch (initializedResource.state) { case "errored": const errorValue = initializedResource(); break; case "ready": const readyValue = initializedResource(); break; case "refreshing": const refreshingValue = initializedResource(); break; // this is weird but it works type Test = Assert<Equals<typeof errorValue, never>> & Assert<Equals<typeof readyValue, string>> & Assert<Equals<typeof refreshingValue, string>>; }}
Version Info