The concept is wrong (or I don't get it)
See original GitHub issueThere 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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@thecotne This is pretty clear, thank you.
Just in case, there is a syntax in TypeScript for immutable arrays:
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<>
.Sounds reasonable, my bad.