Skip to content

Passing false to serialize gives typescript error #465

Open

Description

@MasterOdin

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.

Activity

  1. wodCZ commented on Jul 26, 2022

    @wodCZ
    Collaborator

    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!

  2. MasterOdin commented on Jul 26, 2022

    @MasterOdin
    ContributorAuthor

    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.

  3. pzuraq commented on Sep 25, 2022

    @pzuraq

    👋 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions