When I’ve talked to people who develop and use TypeScript, the general impression I’ve gotten from them is that TS is actually as much about tooling as it is about correctness. TS enables things like autocomplete, jump to definition, and quick access to documentation, and it does that by leveraging the typing information to “see through” the dynamic dispatch that makes that sort of thing infeasible in dynamically-typed JavaScript. The TS type system does provide some correctness benefits, don’t get me wrong, but where ease of use and correctness are in conflict, usually ease of use is preferred.
This is true even with all the “strictness” options enabled, like strict null checking. For some examples of TypeScript unsoundness, see [1] and [2]. Flow is actually generally a lot better than TS in this regard (though it does still have some major soundness holes), but it looks like TS has basically “won,” for better or for worse. But again: TS won because its tooling was better, not because its ability to actually typecheck JS programs was better, which I think reinforces what I said in the previous paragraph on what TS is really about.
[1] https://twitter.com/lexi_lambda/status/1068704405124452352
[2] https://twitter.com/lexi_lambda/status/1068705142109810688
type NonEmptyArray<T> = [T, ...T[]];
That is: A type of "NonEmptyArray" of T is an array of one element of type T, followed by any number of type-T elements.
Here is the old representation:
export interface Cfg {
blocks: CfgBlock[];
}
And here is the new one: export interface Cfg {
head: CfgBlock;
tail: CfgBlock[];
}
Switching to this new representation allowed to then simplify code manipulating this graph. This representation is also simple enough that most schema validators can represent it.You still have to note that with this representation you no longer have a single array. It's not an issue in my case but your situation may vary. You may try the tuple-based solution mentioned in a sibling comment if you need to have a single array.
[0] https://github.com/open-flash/avm1-types/commit/ec85efab8c9e...