question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It is not allowed to push a number to the array foo in {foo: []}

See original GitHub issue

TypeScript Version: Playground

Search Terms: never[]

Code

const foo = { foo: [] };
foo.foo.push(1);

const bar = [];
bar.push(1);

Expected behavior: TypeScript should infer foo to be of type { foo: any[] } and therefore foo.foo.push(1); should be allowed like bar.push(1); is allowed

Actual behavior: Line 2: Argument of type ‘1’ is not assignable to parameter of type ‘never’.

Playground Link: https://www.typescriptlang.org/play/index.html#src=const foo %3D { foo%3A [] }%3B foo.foo.push(1)%3B const bar %3D []%3B bar.push(1)%3B

Related Issues:

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
AnyhowStepcommented, Jan 14, 2019

I tend not to use const x = [];. When I do need to initialize a variable to an empty array, I tend to be explicit with the data type.

So, I’d use, const x : /*insert-data-type-here*/[] = []

And never[] is assignable to /*insert-data-type-here*/[] because never is assignable to all types.


Seems like @fatcerberus has a link that shows what you are seeing is intended behaviour, though

1reaction
fatcerberuscommented, Jan 14, 2019

For bar, TS “evolves” the type with each .push(). So it starts out as any[] but then evolves to number[]. Push a string afterwards and it may then evolve to (number | string)[], etc. I don’t believe that works with [] in object literals, though–after the initial definition (which is inferred as never[] for some reason), the type cannot change further.

Source: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html

A variable declared with no type annotation and an initial value of [] is considered an implicit any[] variable. However, each subsequent x.push(value), x.unshift(value) or x[n] = value operation evolves the type of the variable in accordance with what elements are added to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript syntax: var array = [].push(foo); - Stack Overflow
Array.prototype.push() always returns the new number of elements in the array. It does not return this instance or a new Array instance.
Read more >
Array.prototype.push() - JavaScript - MDN Web Docs
The push() method adds one or more elements to the end of an array and returns the new length of the array. Try...
Read more >
array_push - Manual - PHP
I've done a small comparison between array_push() and the $array[] method and the $array[] seems to be a lot faster. <?php $array =...
Read more >
Arrays - The Modern JavaScript Tutorial
Arrays. Objects allow you to store keyed collections of values. ... The pop method does not need to move anything, because other elements ......
Read more >
Arrays - D Programming Language
A static array with a dimension of 0 is allowed, but no space is allocated for ... error, '$' is not defined, since...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found