3 ms·
> but the types in TS are always _logically_ correct. As in absolutely correct Erm... type Test = Array<number>; const xs: Test = []; const x = x
by azangru 4y ago
> but the types in TS are always _logically_ correct. As in absolutely correct
Erm...
type Test = Array<number>;
const xs: Test = [];
const x = xs[0];
What's the type of x, according to typescript's default behavior? It's number [0]. What's the logically correct type? Some sort of a Maybe<number>, which typescript doesn't have; so a number|undefined instead. Most people don't use typescript at that level of soundness, both because it would be painful, and because typescript doesn't have it as a default.
[0] - https://www.typescriptlang.org/play?#code/C4TwDgpgBAKhDOwoF4oEEBOGCGIA8AdgK4C2ARhBgHwDcAUHQMYD2BiUAHvAFywJKoA2gF16LNkg4pO8QQAZRdIA https://www.typescriptlang.org/play?#code/C4TwDgpgBAKhDOwoF4...
- mpawelski 4y agothere is --no-unchecked-indexed-access option for it. Which is not on by default or in --stric mode because indeed in practise it's painfull https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#no-unchecked-indexed-access https://devblogs.microsoft.com/typescript/announcing-typescr...
- dclowd9901 4y agoI’ve never found it to be overly painful. If you’re accessing values this way (on an index vs defined fields), you’re already treading into iffy territory, structure-wise, and its good to undermine your assumptions about what’s at the index. An extra check might seem very hand-holdy, but I’ve been doing JS long enough now to have seen a large number of errors originate in this class of index access.