-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type Guard Issue with Array.prototype.fill, Array Constructor #31785
Comments
Hmm, this seems challenging to fix. In this case: let a: number[] = new Array(3); TS could in theory use contextual typing to fill in for let a: number[] = new Array(3).fill("foo");
// which is basically equivalent to:
let tmp = new Array(3);
let a: number[] = tmp.fill("foo"); How do we know what type |
We could potentially expand the evolving array type logic to track values from |
Even though this is an uncommon pattern, I believe that there should have some way to warn that the array will be implicitly I got stuck for at least 15 minutes trying to figure out what's wrong with a similar snippet of code if (someCondition) {
// set actual data
setObjDataToBin(getObjectDataWithGoodTypes());
}
else {
// set placeholders
setObjDataToBin(new Array(count).fill({x: 0.5, y: 0.5, zoom: 1}));
}
// somewhere else in the codebase
function setObjDataToBin(obj: {x: number, y: number: z: number}[]): Float32Array {
// some code
}
|
I may misunderstand the nuance/purpose of That means that, in my mind, the following should throw a warning/error when const foo = new Array(3); since its inferred (i.e., implicit) type is Would it be possible to add a |
@markrian The compiler and its options aren't the problem here, it's this line: Line 1375 in 05d59a1
form the array constructor definitions Lines 1374 to 1383 in 05d59a1
if you don't specify a type, it implicitly uses the constructor which has I fear that it is too late for this to change as it will introduce many breaking changes in existing codebases. Maybe in typescript v4? We'll see... |
What is the reason for the Array constructor not returning an |
Actually I think that the typings of https://github.com/microsoft/TypeScript/blob/main/lib/lib.es2015.core.d.ts#L45-L53 are not correct. I think that they should be
|
I just ran into it today. |
Agreed with the above comment. The issue is Pinging @RyanCavanaugh, as you seem to maybe be able to resolve this. Would a quick PR would be accepted? |
Oh also, I wrote this simple function to resolve this for my codebase:
|
Typegurd Issue with Array.prototype.fill, Array Constructor
TypeScript Version: 3.5.1
Search Terms:
Code
Actual behavior:
This code above is accepted because
new Array(3)
returnsany[]
, and sofill("foo")
returnsany[]
.I know giving type explicitly to Array like
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
inlib.es2015.core.d.ts
likefill<S extends T>(value: S, start?: number, end?: number): S[];
, thenOption 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 returnsany[]
".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
The text was updated successfully, but these errors were encountered: