Yeah but why doesnt it infer data to be type: ```t...
# off-topic
j
Yeah but why doesnt it infer data to be type:
Copy code
ts
{Key:string}
instead it infers it to be:
Copy code
ts
{ Key: string } | null
Me and my friend researched this further and came to conclusion that almost all of the storage methods are wrongly typed, they are being cast wrongly. We think its due to typescript version 4.07 which is bugged, we will make pull request and ask you to update typescript version aswell. This is the bug we are talking about : https://github.com/microsoft/TypeScript/issues/48037 This is example of wrongly typed method in the storage library:
Copy code
ts
getPublicUrl(
    path: string
  ): {
    data: { publicURL: string } | null
    error: Error | null
    publicURL: string | null
  } {
    try {
      const _path = this._getFinalPath(path)
      const publicURL = `${this.url}/object/public/${_path}`
      const data = { publicURL }
      return { data, error: null, publicURL }
    } catch (error) {
      return { data: null, error, publicURL: null }
    }
  }
Is typed as:
Copy code
ts
{
    data: { publicURL: string } | null
    error: Error | null
    publicURL: string | null
  }
But should be:
Copy code
ts
{
  data: null;
  error: Error;
  publicURL: null;
}
| {
data: { publicURL: string };
error: null;
publicURL: string;
};
This is a huge inconvenience for users of this library and requires people that want to use typescript correctly to write a lot of boilerplate.
j
keeping this in a thread for better house keeping
I'm not that familiar with this but the following
{ Key: string } | null
and types that include null are perhaps because the request made with the user's authorization might not have permission to read/write that storage bucket path because of a row level security policy. So in that circumstance, it might be possible to have no error, but also return null, since that user fails the policy check. I can't really comment on wether that is ideal DX, but perhaps that is why there are optional null types. Again, this isn't really my area but that would be my guess.
j
It will return an error in that case, If not then the whole library itself is typed wrongly not only this part of code
I can make pull requests for that but I would love for someone to confirm what I’m saying