Skip to content

Iterable gets resolved incorrectly if value is passed directly and element has method聽#55489

Description

@InSyncWithFoo

馃攷 Search Terms

"iterable", "literal", "method"

馃晽 Version & Regression Information

馃捇 Code

// Attribution: https://stackoverflow.com/a/75687384
type NonStringIterable<T> = 
  T extends string ? never : T extends Iterable<any> ? T : never;

declare function doSomething<T>(value: NonStringIterable<T>): T;

const o = { foo() {} };

doSomething('value');
doSomething(['v']);
doSomething([o]);
doSomething([{ foo() {} }]);

馃檨 Actual behavior

const o = { foo() {} };

doSomething('value');
//          ~~~~~~~
// Argument of type 'string' is not assignable to parameter of type 'never'. (2345)
// (correct)

doSomething(['v']); // Fine (correct)
doSomething([o]);   // Also fine (correct)

doSomething([{ foo() {} }]);
//          ~~~~~~~~~~~~~~
// Argument of type '{ foo(): void; }[]' is not assignable to parameter of type 'never'. (2345)
// (incorrect)

doSomething([o]) works correctly, yet directly passing the same object as o causes an error. This error happens regardless of the method being deeply nested or not. Interestingly, empty object ({}) and arrow function ({ foo: () => {} }) are fine. The same goes with a separately declared method.

馃檪 Expected behavior

Only the first example should cause an error.

Activity

  1. Andarist commented on Aug 24, 2023

    @Andarist
    Contributor

    My gut feeling is that this is related to the fact that this method is context-sensitive (since it's a method that has access to the this parameter). The containing object is blocked from being a source of inference based on that. Thus the constraint gets assigned to the inferred type here (since there are no other inference sources present) - that's unknown, and if we run this conditional type with unknown as T we get never as the result.

  2. andrewbranch commented on Aug 24, 2023

    @andrewbranch
    Member

    This is one example of the kind of problem tracked at #47599.

  3. typescript-bot commented on Aug 27, 2023

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" 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 22, 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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions