effect
effect copied to clipboard
From Discord: TypeScript accepts number keys in `Record`, but runtime fails with number keys. Consider restrict...
Summary
The messages discuss an issue with using Schema.Record from the Effect library in TypeScript. Specifically, the problem is with S.Record accepting number-typed keys in its type system, but failing at runtime when these number keys are actually used. In the provided code examples:
StringKeyis defined as a branded string type and works correctly withS.Record.MyStringRecordis a record schema usingStringKeyas the key type andValue(a branded number type) as the value.NumberKeyis defined as a branded number type, but using it inMyNumberRecordleads to runtime failure, showcasing the problem.
Key takeaways:
- TypeScript allows number-typed keys in the type system, but JavaScript (and thus Effect at runtime) does not support them as intended when using
S.Record. - A potential improvement would be to restrict
S.Recordto only accept valid property keys (strings or symbols in JavaScript) to avoid such runtime errors. - Users need to be cautious about the differences between TypeScript's type system and JavaScript runtime behavior, particularly concerning object keys.
Discord thread
https://discord.com/channels/795981131316985866/1355351885036519454