Skip to content

Promise resolution type cannot be inferred inside a function with conditional typesΒ #48234

@AmirAsghary

Description

@AmirAsghary

Bug Report

πŸ”Ž Search Terms

conditional, generic

πŸ•— Version & Regression Information

03/12/2022

  • This is a crash
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about common bugs that aren't bugs

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Base64Data = FileReader['result'];

// fill-in for actual functions
function base64ToFile (x: any) { return new File([], ''); }
function fileModifier (y: File) { return new Promise<Blob>(res => res('' as unknown as Blob)); }

export function downsizeImageFile<T extends boolean = false>(
  imgFile: File,
  preserveBase64?: T,
  maxSize = 640
): Promise<T extends true ? Base64Data : File> {
  return new Promise(async resolve => {
      const resizedBlob = await fileModifier(imgFile);
      const reader = new FileReader();
      reader.readAsDataURL(resizedBlob);

      reader.onloadend = () => {
        resolve(preserveBase64 ? reader.result : base64ToFile(base64file)); // problem: the correct type cannot be inferred from the conditional statement
      };
  });
}

export async function downsizeImage<T extends boolean = false>(
  base64Img: Base64Data,
  preserveBase64?: T,
  maxSize = 640
): Promise<T extends true ? Base64Data : File> {
  return downsizeImageFile(base64ToFile(base64Img), preserveBase64, maxSize);
}

πŸ™ Actual behavior

The promise resolution type is not inferred correctly from the conditional statement.

πŸ™‚ Expected behavior

The promise resolution type must be Base64Data if the preserveBase64 argument is true, otherwise it must be File

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions