Skip to content

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

@InSyncWithFoo

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.

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