Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type: make type inference simpler #1908

Merged
merged 1 commit into from Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions infinite/types.ts
Expand Up @@ -6,9 +6,7 @@ export type SWRInfiniteFetcher<
Data = any,
KeyLoader extends SWRInfiniteKeyLoader = SWRInfiniteKeyLoader
> = KeyLoader extends (...args: any[]) => any
? ReturnType<KeyLoader> extends readonly [...infer T]
? (args: T) => FetcherResponse<Data>
: ReturnType<KeyLoader> extends infer T | null | false | undefined
? ReturnType<KeyLoader> extends infer T | null | false | undefined
? (args: T) => FetcherResponse<Data>
: never
: never
Expand Down
6 changes: 1 addition & 5 deletions src/types.ts
Expand Up @@ -7,11 +7,7 @@ export type BareFetcher<Data = unknown> = (
export type Fetcher<
Data = unknown,
SWRKey extends Key = Key
> = SWRKey extends () => readonly [...infer Args]
? (args: Args) => FetcherResponse<Data>
: SWRKey extends readonly [...infer Args]
? (args: Args) => FetcherResponse<Data>
: SWRKey extends () => infer Arg | null | undefined | false
> = SWRKey extends () => infer Arg | null | undefined | false
? (args: Arg) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
? never
Expand Down
8 changes: 4 additions & 4 deletions test/type/fetcher.ts
Expand Up @@ -78,7 +78,7 @@ export function useTuple() {
export function useReadonlyTuple() {
useSWR([{ a: '1', b: { c: '3' } }, [1231, '888']] as const, keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand All @@ -94,7 +94,7 @@ export function useReadonlyTuple() {
truthy() ? ([{ a: '1', b: { c: '3' } }, [1231, '888']] as const) : null,
keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand All @@ -111,7 +111,7 @@ export function useReadonlyTuple() {
truthy() ? ([{ a: '1', b: { c: '3' } }, [1231, '888']] as const) : false,
keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down Expand Up @@ -300,7 +300,7 @@ export function useReturnReadonlyTuple() {
() => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const,
keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down
10 changes: 5 additions & 5 deletions test/type/option-fetcher.ts
Expand Up @@ -80,7 +80,7 @@ export function useReadonlyTuple() {
useSWR([{ a: '1', b: { c: '3' } }, [1231, '888']] as const, {
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand All @@ -98,7 +98,7 @@ export function useReadonlyTuple() {
{
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand All @@ -117,7 +117,7 @@ export function useReadonlyTuple() {
{
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down Expand Up @@ -318,7 +318,7 @@ export function useReturnReadonlyTuple() {
useSWR(() => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const, {
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down Expand Up @@ -376,7 +376,7 @@ export function useReturnReadonlyTuple() {
useSWRInfinite(() => [{ a: '1', b: { c: '3' } }, [1231, '888']] as const, {
fetcher: keys => {
expectType<
[
readonly [
{
readonly a: '1'
readonly b: {
Expand Down