-
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
Empty array literals without contextual type can lead to unsound index accesses #57419
Comments
That's what |
This doesn't really help much, since |
This has always bothered me too. That aligns better with the semantics of |
The problem is that if you infer const x: number[] = []; // error because unknown[] isn't assignable to number[] |
Well, not exactly Iβm modeling my expectation on how the return type of |
This one uses inference from a contextual return type to achieve this effect: const a = Array.of() // unknown[]
const b: string[] = Array.of() // string[] It is a more specific situation here since inference is involved and this example just happens to work this way. It doesn't have anything to do with arrays and array literal expressions (and their "fresh" types).
Yeah, I would imagine this being a possibility here. I understand how it would be kind of a sidestep from general rules in how all of this currently works. On the other hand, if
That is fine but I think that people mostly understand that this comes into play when dealing with |
Pinging @RyanCavanaugh back here because I'm almost positive I remember him explaining the exact reasoning for the |
Nevermind, I found his explanation: #51853 (comment) |
(reading my own comment) ahh now I get it |
That can be terrifically unsound since it doesn't check whether the type is being used contravariantly. The element type of the array is bounded below by const obj = { a: []}
const pushA = <T,>(blah: {a: T[]}, a:T)=>{
blah.a.push(a)
}
pushA(obj, 4)
pushA(obj, "foo")
// obj.a is now an array containing [4, 'foo'] but typed as never[] Then again, this does work in a covariant context: |
I have a lot of bad news about supertype-aliased writes in TypeScript |
π Search Terms
empty array literal contextual type declared type index access implicitnevertype
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.4.0-dev.20240215#code/MYewdgzgLgBCBGArGBeGBvAUDGBDATvgFwwDaAugDSYC+A3JpqJLAogOoCWUAFgJJgAJgFMAHiQBKw0PkEAeaPk5gA5pRjwQIADbDcYAHyoM9Rs2gxh242wB0BfKQAM5Budj5hEAK7bYaNi5eARFRUitXTAB6KJwcAD0AfiA
π» Code
π Actual behavior
never
element type allows for just anything to happen here sincenever
is assignable to everythingπ Expected behavior
I wouldn't expect this empty array literal to allow for this. Maybe it would be better to use an empty tuple in that situation? This would prevent obtaining
never
by indexing this array and thus it would prevent such accidents.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: