Don't allow to iterate over 'never'#22964
Conversation
weswigham
left a comment
There was a problem hiding this comment.
I mean, conceptually never is iterable. It's everything, kinda like any. But this is a good change - I just think the error message could be improved.
| function getIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, allowAsyncIterables: boolean, checkAssignability: boolean): Type { | ||
| function getIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, allowAsyncIterables: boolean, checkAssignability: boolean): Type | undefined { | ||
| if (inputType === neverType) { | ||
| reportTypeNotIterableError(errorNode, allowAsyncIterables); |
There was a problem hiding this comment.
Hmmm... the diagnostic message Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator doesn't mention what type we're erroring on. Might be an improvement to include the type in the error, especially since typing x[Symbol.iterator]() isn't going to produce an error when x is never (and will have a "return type" assignable to an iterator).
|
@weswigham Good to go? |
|
Yeah, looks good. |
|
Given that we are special casing declare const nope: never;
declare const anArray: any[];
declare const anObject: { [key: string]: any };
declare function aMethod(arg: string): string;
anArray[nope]; // $ExpectError
anObject[nope]; // $ExpectError
aMethod(nope); // $ExpectError
const hello = "hello " + nope; // $ExpectError
const meaningOfLife = 42 + nope; // $ExpectError |
|
@ericanderson Could you open a new issue? |
Currently the following has no compile error:
I meant
string | string[]. The problem would be solved ifneverweren't considered iterable.