fp-ts
fp-ts copied to clipboard
Make JsonRecord type safe
The JsonRecord type currently allows you to access any string key, but this isn't type safe. Looking at the commit history the type came from https://github.com/Microsoft/TypeScript/issues/1897#issuecomment-338650717, and does seem to have been copied over to many other libraries.
This PR changes the values of a JsonRecord to also possibly be undefined, covering the fact that the key might not be set.
This does mean that it's possible to set keys as undefined, even though this type doesn't exist in JSON (stringify will throw these keys away). This actually fixes my problem in https://github.com/gcanti/io-ts/pull/628#issuecomment-1084597642 (which was complicated to try and get round in https://github.com/gcanti/io-ts/pull/639).
dtslint failed in CI; locally it was only running up to TS 4.1. Looks like the library isn't pinned so I had an outdated version.
The JsonRecord type currently allows you to access any string key, but this isn't type safe
IIRC there's a tsconfig option for that (noUncheckedIndexedAccess)
@gcanti Ah, that's definitely something to enable generally (and does fix the accessing problem). Thanks.
It doesn't, however, get around https://github.com/gcanti/io-ts/pull/628#issuecomment-1084597642. Should I close this and reopen https://github.com/gcanti/io-ts/pull/639?