-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Object mistyped as Array when Array is spread into Object #60063
Comments
Enumerability/ownness is not tracked in the type system, so we don't have any mechanism to recognize that this won't work. See #9726 |
I'm curious what the actual bug you had was. It might worth erroring when spreading something sufficiently arraylike in an object spread position. |
@RyanCavanaugh the bug was accidentally passing an Object to a function that wanted an array and that function tried to use the array methods which did not exist. that's the screenshot I attached above. |
Were you calling |
@RyanCavanaugh I was doing the spread I have in the above example
but this was a mistake on my part. I would have wanted to do
or even
but I over looked it and would have hoped typescript would have given me a type error. |
though if you look at the generated JS from the playground it seems to be doing an Object.assign after generation. |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Just to add to this: it's really easy to run into this when refactoring a property from an object into an array. This causes Typescript to generate Javascript that's broken (playground) without compilation errors: type WithArr = { a: string; b: Array<{ c: string; }> };
const arr: Array<{ c: string; }> = [{ c: "test" }];
const test = { a: "foo", b: { ...arr } };
console.log(test.b.map(a => a));
// test.b.map is not a function |
dupe of #34780 |
π Search Terms
destructure array in to object
array spread in to object
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/C4TwDgpgBAYg9nKBeKBvKAjAhgJwFxQDOwOAlgHYDmUAvgNwCwAUAGYCu5AxsKXOVC3IAKYAXhwA2gF0AlGig4IwNjn7AAdAFssYISGQA+KAEJjIOTWac+xBcnnrHE9NnxQARMTJV3tKbUZWYRwZOiA
π» Code
π Actual behavior
Foo
Allows me to pass anObject
when it wants anArray
. whenfn
is called withObject
it throws an error because there is nomap
on anObject
π Expected behavior
There should be a typescript error when passing an
Object
to function that wants anArray
Additional information about the issue
In the d.ts file you can see how it has created a type for
r
that looks like an array.The text was updated successfully, but these errors were encountered: