Skip to content

useLinkedState infers unknown when derive reads previous with sourceEqual #1830

Closed

Description

@domgan-oai

useLinkedState can infer its value as unknown when the derivation reads previous and sourceEqual is supplied, even though the callback returns a boolean. This reproduces against the 0.10.0 source at 082412f2410405651dcee0c4d077ddd056c26573 with TypeScript 5.9.3 in strict mode. It also reproduces importing the built runtime declaration directly, so it does not depend on the template compiler.

Minimal reproduction:

import { useLinkedState } from "octane";

export function example() {
  const [changed] = useLinkedState(
    { key: "sample" },
    (source, previous) =>
      previous !== undefined && source.key !== previous.source.key,
    { sourceEqual: (a, b) => a.key === b.key },
  );

  const result: boolean = changed;
  return result;
}

Expected: the inferred linked value and previous.value are boolean.

Observed: strict TypeScript reports the inferred value as unknown (TS2322); uses of previous or its inferred value can also report TS18046 or TS2345.

Workaround: explicitly annotate the derive callback's return type as boolean. It would help callers if the API signature could infer that return type without annotation while still typing previous. This is a declaration/inference issue, not an observed runtime failure.

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