mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.08k stars 30 forks source link

Why is noUncheckedIndexedAccess mandatory for Option #68

Open Nodonisko opened 1 year ago

Nodonisko commented 1 year ago

Hi, I am using ts-belt in legacy code-base and there is this message in docs:

Adding noUncheckedIndexedAccess to your tsconfig.json is mandatory for the Option type!

Why it's neccessary, will it not work at all or it will break in some cases? Enabling noUncheckedIndexedAccess in our codebase is not a option because it will throw like in milion places.

mobily commented 1 year ago

Why it's neccessary

because TS doesn't infer the type correctly in this specific case and it points to (for instance) string | undefined instead of Option<string>

Enabling noUncheckedIndexedAccess in our codebase is not a option because it will throw like in milion places.

to me having this option enabled is a must-have for any TS project, it provides another layer of safety (and in my opinion, this option should be enabled by default in TS)

Nodonisko commented 1 year ago

Thanks for explanation. I agree with better safety, but enabling it in legacy project is nearly impossible.

Nodonisko commented 1 year ago

Played with it little bit more, but noUncheckedIndexedAccess doesn't seems to work with anything that string literals for object/array keys.

const { min, max } = getMinMaxIndex(arrayOfNumbers);
if(arrayOfNumbers[min]) {
  // this will throw anyway because of noUncheckedIndexedAccess
  console.log(arrayOfNumbers[min])
}

There is lot of opened issues about this for years in TS repo but it doesn't seems to move anywhere. But it's probably no big deal for us we can go with Result in most cases.