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.

The concept is wrong (or I don't get it)

See original GitHub issue

There is an example in the readme:

const numbers: Array<number> = [];

// HegelError: Type "Array<number>" is incompatible with type "Array<number | string>"
const numbersOrStrings: Array<string | number> = numbers;

The error is completely wrong and here is why. The type Array<number> means «an array which every element is a number». The type Array<string | number> means «an array which every element is a string or a number». The first type is assignable to the second because «a number» is «a string or a number». For example, array [1, 2, 3] matches the description «an array which every element is a string or a number». Thus numbers CAN be assigned to numbersOrStrings.

The second part of the example is correct. The example gives only 1 error in runtime (the one in the second part).

This lack of flexibility is useless and bring inconveniences.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Finessecommented, Jun 1, 2020

@thecotne This is pretty clear, thank you.

Just in case, there is a syntax in TypeScript for immutable arrays:

function countSum(arr: readonly <Array<string | number>>) {

It means that a value can’t be add/changed/removed from the array through square brackets or the Array prototype methods. This syntax looks more pleasant than $Immutable<>.

2reactions
Finessecommented, May 31, 2020

Sounds reasonable, my bad.

Read more comments on GitHub >

github_iconTop Results From Across the Web

9 Words and Phrases You're Probably Using Wrong
Sure, saying the wrong word (usually) isn't a game-changer. ... Here, excerpted from our new book, That Doesn't Mean What You Think It...
Read more >
You Have No Idea How Wrong You Are - YouTube
The problem with standing on the shoulders of giants is that we are liable to mistake the new horizon for the edge of...
Read more >
Really Cheap Thoughts
Murphy's First Law: Anything that can go wrong will go wrong. ... Field experience is something you don't get until just after you...
Read more >
What Are Statements? Definition and Examples
This question does not express something that can be true or false. It makes no sense to respond “true” or “false” when you...
Read more >
Don't get what's wrong with blackface? Here's why it's so ...
To many, it's obvious that it's a lazy, non-funny costume bad idea with a depressing history that is the opposite of celebratory.
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