Skip to content

Type inference for advanced linkedSignal signature #60423

Closed

Description

@JoostK

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

The signature of linkedSignal that takes an object with source and computation properties does not allow for proper type inference, given the cyclic nature of the previous argument of the computation. Even though previous.value uses NoInfer to block TypeScript from picking up inferences from this position, TypeScript is still unable to infer type arguments if they are omitted:

const user = signal('Joost');
const linked = linkedSignal({
  source: user,
  computation: (user) => user.length,
});

Here, linked is inferred as WritableSignal<unknown> instead of WritableSignal<number>.

Please provide a link to a minimal reproduction of the bug

TypeScript playground

Activity

  1. added
    area: core
    core: reactivity
    on Mar 17, 2025
  2. modified the milestone: needsTriage on Mar 17, 2025
  3. Harpush commented on Mar 17, 2025

    @Harpush

    @JoostK See discussion microsoft/TypeScript#60922
    And also PR for partial fix
    microsoft/TypeScript#60964

  4. self-assigned this
    on Mar 17, 2025
  5. added 2 commits that reference this issue on Apr 13, 2025
    cf9dd77
    ede8e49
  6. JoostK commented on Apr 13, 2025

    @JoostK
    MemberAuthor

    We've attempted to find alternative declarations for linkedSignal, but none of them behaved entirely as we'd want. Therefore, #60857 documents the current requirement that type arguments have to be specified when using the previous argument.

    Mentioning two approaches here that didn't quite work out:

    1. Introduce an additional type argument to deprioritize the previous type

    export function linkedSignal<S, D, P extends D = D>(options: {
      source: () => S;
      computation: (source: NoInfer<S>, previous?: {source: NoInfer<S>; value: NoInfer<P>}) => D;
      equal?: ValueEqualityFn<NoInfer<D>>;
    }): WritableSignal<D>;

    This has the downside of exposing an outer type argument and isn't quite accurate in the sense that P could incorrectly be a subtype of D.

    2. Make the computation callback generic

    export function linkedSignal<S, D>(options: {
      source: () => S;
      computation: <P extends D>(source: NoInfer<S>, previous?: {source: NoInfer<S>; value: NoInfer<P>}) => D;
      equal?: ValueEqualityFn<NoInfer<D>>;
    }): WritableSignal<D>;

    This degree of freedom does allow for type inference in some cases, but the P type isn't always inferred as desired:

    TypeScript playground

    Notice how the P generic type argument has not been locked in to correspond with D.

  7. Harpush commented on Apr 13, 2025

    @Harpush

    @JoostK is there a way to fix the missing intelisense for the object overload? Currently for me at least it doesn't suggest source and computation

  8. JoostK commented on Apr 13, 2025

    @JoostK
    MemberAuthor

    I'm not aware of that limitation—interestingly, Jetbrains' engine (IntelliJ and WebStorm etc.) doesn't have that problem—and there is nothing to be done about that. There's a few TS issues that appear related.

  9. Harpush commented on Apr 14, 2025

    @Harpush

    I think microsoft/TypeScript#60964 is available in typescript 5.8.3.
    Might make sense to mention that. With the fix you can declare the computation return type only and avoid specifying generic arguments altogether thus source can be inferred.

  10. added 2 commits that reference this issue on Apr 14, 2025
    f6e52f2
    79ff75f
  11. added a commit that references this issue on Apr 14, 2025
    27bf809
  12. added a commit that references this issue on Apr 14, 2025
    6b9434a
  13. angular-automatic-lock-bot commented on May 15, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corecore: reactivitycross-cutting: signals

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions