Skip to content
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

Closed
StefanDBTLabs opened this issue Sep 25, 2024 · 9 comments
Closed

Object mistyped as Array when Array is spread into Object #60063

StefanDBTLabs opened this issue Sep 25, 2024 · 9 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@StefanDBTLabs
Copy link

πŸ”Ž Search Terms

destructure array in to object
array spread in to object

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about arrays and objects

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.2#code/C4TwDgpgBAYg9nKBeKBvKAjAhgJwFxQDOwOAlgHYDmUAvgNwCwAUAGYCu5AxsKXOVC3IAKYAXhwA2gF0AlGig4IwNjn7AAdAFssYISGQA+KAEJjIOTWac+xBcnnrHE9NnxQARMTJV3tKbUZWYRwZOiA

πŸ’» Code

type Foo = { bar: string };
function fn(t: Foo[]) { return t.map(y => !!y) }
const r = { ...[{ bar: "string" }] };
fn(r);

πŸ™ Actual behavior

Foo Allows me to pass an Object when it wants an Array. when fn is called with Object it throws an error because there is no map on an Object

image

πŸ™‚ Expected behavior

There should be a typescript error when passing an Object to function that wants an Array

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.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 25, 2024
@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh
Copy link
Member

I'm curious what the actual bug you had was. It might worth erroring when spreading something sufficiently arraylike in an object spread position.

@StefanDBTLabs
Copy link
Author

@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.

@RyanCavanaugh
Copy link
Member

Were you calling assign or doing a spread?

@StefanDBTLabs
Copy link
Author

StefanDBTLabs commented Sep 25, 2024

@RyanCavanaugh I was doing the spread I have in the above example

const r = { ...[{ bar: "string" }] };
fn(r);

but this was a mistake on my part. I would have wanted to do

const r = [ ...[{ bar: "string" }] ];
fn(r);

or even

const r = [{ bar: "string" }];
fn(r);

but I over looked it and would have hoped typescript would have given me a type error.

@StefanDBTLabs
Copy link
Author

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 28, 2024
@martijnarts
Copy link

martijnarts commented Oct 21, 2024

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

@jcalz
Copy link
Contributor

jcalz commented Feb 27, 2025

dupe of #34780

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants