microsoft / microsoft/TypeScript
Type Guard Issue with Array.prototype.fill, Array Constructor
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Typegurd Issue with Array.prototype.fill, Array Constructor
TypeScript Version: 3.5.1
Search Terms:
- Array.prototype.fill
- fill
- ArrayConstructor
- array implicit any
Code
const foo: number[] = new Array(3).fill("foo"); // accepted
Actual behavior:
This code above is accepted because new Array(3) returns any[], and so fill("foo") returns any[].
I know giving type explicitly to Array like
const foo: number[] = new Array<number>(3).fill("foo"); // error
would work, but I believe the compiler should reject the first one. (This is TypeScript.)
Expected behavior:
Option A. Array.prototype.fill returns narrow type
replace declaration of Array.fill in lib.es2015.core.d.ts like fill<S extends T>(value: S, start?: number, end?: number): S[];, then
const foo: number[] = new Array(3).fill("foo");
// error: "Type 'string[]' is not assignable to type 'number[]'."
// because `fill` is resolved as `fill<string>(value: string, .....): string[]`
const bar = new Array(3).fill(0); // `bar` is resolved as `number[]`
bar.fill("bar"); // error: "Type '"bar"' is not assignable to type 'number'."
const baz: (number|string)[] = new Array(3).fill(0); // accepted.
baz.fill("baz"); // accepted.
Option B. Array Constructor never return implicit any
The problem is that Array constructor returns any[] implicitly. The first code is accepted even with --strict or any other options. It means we always have to care about "Array constructor returns any[]".
Something like #26188 could solve this issue.
Playground Link:
https://www.typescriptlang.org/play/#src=const%20foo%3A%20number%5B%5D%20%3D%20new%20Array(3).fill(%22foo%22)%3B
Related Issues:
#29604
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 example in the linked TypeScript Playground, then read the Array and ArrayConstructor declarations in lib.es2015.core.d.ts. Review related issues #29604 and #26188 and determine which proposed behavior is intended. Done means the accepted behavior is specified and covered by the relevant TypeScript tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100