I’ve recently been using TypedArrays more for manual memory management / custom data storage, and find myself somewhat struggling with the lack of good typing support in TypeScript w.r.t. TypedArrays. eg. Even if I can define a number value type union such as
type Kind = 0 | 1 | 2 | 3 | 4 | 5;
I have no way of defining a Uint8Array that contains only this Kind type. After working around this with arr[i] as Kind casting and getting bit a few times by refactorings breaking the code but the as cast types not catching this, I opened up a feature suggestion for TypeScript (the pits, I know...) to support this. One response I got was to say that this is a niche use-case, and that made me wonder:
What are JavaScript/TypeScript developers actually using TypedArrays…
I’ve recently been using TypedArrays more for manual memory management / custom data storage, and find myself somewhat struggling with the lack of good typing support in TypeScript w.r.t. TypedArrays. eg. Even if I can define a number value type union such as
type Kind = 0 | 1 | 2 | 3 | 4 | 5;
I have no way of defining a Uint8Array that contains only this Kind type. After working around this with arr[i] as Kind casting and getting bit a few times by refactorings breaking the code but the as cast types not catching this, I opened up a feature suggestion for TypeScript (the pits, I know...) to support this. One response I got was to say that this is a niche use-case, and that made me wonder:
What are JavaScript/TypeScript developers actually using TypedArrays for, if number is a sufficient value type in all but niche use-cases? If you have open-source code examples to point to, that’s awesome, but I’m interested in user stories / written word as well.