Skip to content

Generic interface with a union type involving a generic function causes problems with unknown #39843

Closed

Description

@zeddic

TypeScript Version: 3.9.2

Search Terms:
union type, generics, generic function with unions, unknown, unknown is not assignable to

Code

interface UpdateFn<State> {
  (draft: State): State;
}

interface Container<State> {
  value: State;
  update(input: State|UpdateFn<State>): void; // <-- union here causes failure
  // update(input: State): void;                            // OK
  // update(UpdateFn<State>): void;                         // OK
  // update(input: State|((input: string) => State)): void; // OK
}

const containers: Array<Container<unknown>> = [];

function addContainer(c: Container<string>): Container<string> {
  containers.push(c);  // <-- error
  return c;
}

Expected behavior:
Container<string> may be added to Array<Container<unknown>>

Actual behavior:

Argument of type 'Container<string>' is not assignable to parameter of type 'Container<unknown>'.
  Type 'unknown' is not assignable to type 'string'.(2345)

Playground Link:
Playground link

Related Issues:

Activity

  1. jack-williams commented on Aug 2, 2020

    @jack-williams
    Collaborator

    This is caused by a variance measurements which marks Container invariant in State because a function of type (x: A) => A is invariant in A. The measurement means that Container<A> is assignable to Container<B> if A is identical to B, which string and unknown are not.

    The reason the union triggers the error is because there is special logic to treat callbacks bivariantly, but the addition of the union to the argument of update no longer marks the argument as a callback, so it gets related strictly.

    I believe this is working as intended, thought it might be slightly tricky to unpick.

    Assuming that you are happy with some (technical) unsoundness, which may be ok if you only call update with the value stored in the container, then one solution is the bivariance hack:

    interface UpdateFn<State> {
      callback(draft: State): State;
    }
    
    interface Container<State> {
      value: State;
      update(input: State | (UpdateFn<State>['callback'])): void;
    }
  2. RyanCavanaugh commented on Aug 20, 2020

    @RyanCavanaugh
    Member

    Jack Williams (@jack-williams) 's analysis is correct as usual

  3. typescript-bot commented on Aug 23, 2020

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions