4 ms·
prices: NonEmptyArray<Price> Doesn't this mean you can't ever remove an element from the array, only add to it? Like, the compiler should throw a type error i
by tantalor 2y ago
prices: NonEmptyArray<Price>
Doesn't this mean you can't ever remove an element from the array, only add to it?
Like, the compiler should throw a type error if you try to pop() from an array with one element.
- bruce343434 2y agoCould also be that pop() can now error out at runtime
- tantalor 2y agoInterestingly, [].pop() does not throw an error.
- MichaelNolan 2y agoTypeScript is just for compile time. You can pop() every element out of the array at runtime with no error. This compiles with no issue - https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwKIFsygIICcsEMQA8AKgHxQC8UA2kQDRQB0TRVAuqwNwBQXAxogGdgUFCGx4QALliJU6MTnwEEAVxQAjCFjKUqARnoAmegGZOPAPQWoAJQgo4AN2jAAFtAA2uIVAgf7EAjAXKLi+AxgcGAAFACU3HyCcP4MHnAA5tGhiiDxUFZQAPIqwGAl0vpGrJbWdg7OUG6e3sJ+AUFQuOm4AJYIIQoSEVFxCfwIAskQqRlZg-h5BcWl5dR61VwFdU4u7lBePm0ogcJdvf3ZQ5Ex8TzjkylpmZcLHPnWy2XAFdVAA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...
- tantalor 2y agoIf that's true, then how is a type like this (non-empty array) useful at all, if it can't be relied on at runtime? TS should say, you can't pop() this array, unless TS can infer it has >1 elements. Otherwise it can enter a state at runtime which doesn't conform to the type. That seems bad!