IssueRaiser

Array.isArray type narrows to any[] for ReadonlyArray<T>

<!-- BUGS: Please use this template. --> <!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript --> <!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md --> **TypeScript Version:** 2.4.1 **Code** ```ts declare interface T { foo: string; } const immutable: T | ReadonlyArray<T> = []; if (Array.isArray(immutable)) { const x = immutable; // Any[] - Should be ReadonlyArray<T> } const mutable: T | Array<T> = []; if (Array.isArray(mutable)) { const x = mutable; // T[] } ``` **Expected behavior:** Should type narrow to ```ReadonlyArray<T>```, or at the very least ```T[]```. **Actual behavior:** Narrows to ```any[]```. Doesn't trigger warnings in noImplicitAny mode either.

Posted by jinder on Jul 7, 2017, 10:42 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

support (Software)

Array.isArray type narrows to any[] for ReadonlyArray<T>

jinderJul 7, 2017, 10:42 AM

<!-- BUGS: Please use this template. --> <!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript --> <!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md --> **TypeScript Version:** 2.4.1 **Code** ```ts declare interface T { foo: string; } const immutable: T | ReadonlyArray<T> = []; if (Array.isArray(immutable)) { const x = immutable; // Any[] - Should be ReadonlyArray<T> } const mutable: T | Array<T> = []; if (Array.isArray(mutable)) { const x = mutable; // T[] } ``` **Expected behavior:** Should type narrow to ```ReadonlyArray<T>```, or at the very least ```T[]```. **Actual behavior:** Narrows to ```any[]```. Doesn't trigger warnings in noImplicitAny mode either.

Comments (0)

No comments yet. Start the discussion.

Comment on this issue