🔎 Search Terms
"iterable", "literal", "method"
🕗 Version & Regression Information
- This is the behavior in every version I tried:
-
-
-
-
-
-
💻 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.