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

Passing false to serialize gives typescript error #465

Open
MasterOdin opened this issue Jul 26, 2022 · 3 comments · May be fixed by #471
Open

Passing false to serialize gives typescript error #465

MasterOdin opened this issue Jul 26, 2022 · 3 comments · May be fixed by #471

Comments

@MasterOdin
Copy link
Contributor

MasterOdin commented Jul 26, 2022

Looking at the Additional Options doc, it gives:

  // Whether to serialize to JSON before/after persisting. Defaults to true.
  serialize?: boolean,

but on trying to pass false, I get the tsc error:

(property) ApolloPersistOptions<NormalizedCacheObject, true>.serialize?: true | undefined
Type 'false' is not assignable to type 'true'.ts(2322)
index.d.ts(20, 5): The expected type comes from property 'serialize' which is declared here on type 'ApolloPersistOptions<NormalizedCacheObject, true>'

To get by this, I'm ignoring the error via a tsc annotation, but that definitely feels hacky and there didn't seem an obvious way to let tsc accept false.

@wodCZ
Copy link
Collaborator

wodCZ commented Jul 26, 2022

Hi, thanks for the report.

I guess I messed something up in the StorageType inference here:

type StorageType<T, TSerialize extends boolean> = TSerialize extends true
? PersistentStorage<string>
: PersistentStorage<T>;

Maybe providing a false branch is required:

declare type StorageType<
  T,
  TSerialize extends boolean
> = TSerialize extends true
  ? PersistentStorage<string>
  : TSerialize extends false
  ? PersistentStorage<T>
  : PersistentStorage<string>;

I'll look into this, but not sooner than by the end of the week. @MasterOdin if you by any chance try working on #464, could you check whether the change above fixes the issue?

Thanks!

@MasterOdin
Copy link
Contributor Author

I tried, and it did not resolve the problem. I think the problem lies with:

TSerialize extends boolean = true

where TS is not smart enough to see that I'm passing it false in usage and bubble that upwards appropriately, rather it's forcing true | undefined from top down. I'm not sure what the best way to resolve this would be.

@pzuraq
Copy link

pzuraq commented Sep 25, 2022

👋 I made a PR over in #471 to address this, TS does need to have the explicit link downward through generics typically, otherwise it'll use the default parameter (it's like calling a function with optional params, if you don't pass in a generic, you'll always get the default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants