microsoft / microsoft/TypeScript
Array.isArray type narrows to any[] for ReadonlyArray<T>
Open
Nobody has claimed this yet.
Domain: lib.d.ts
In Discussion
Suggestion
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.4.1
Code
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the TypeScript 2.4.1 example with Array.isArray, ReadonlyArray, and noImplicitAny. Trace the type narrowing behavior for the immutable and mutable declarations, then verify that the resulting type is no longer any[] and matches the expected narrowing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100