effect icon indicating copy to clipboard operation
effect copied to clipboard

From Discord: TypeScript accepts number keys in `Record`, but runtime fails with number keys. Consider restrict...

Open effect-bot opened this issue 9 months ago • 0 comments

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:

  • StringKey is defined as a branded string type and works correctly with S.Record.
  • MyStringRecord is a record schema using StringKey as the key type and Value (a branded number type) as the value.
  • NumberKey is defined as a branded number type, but using it in MyNumberRecord leads 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.Record to 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

effect-bot avatar Mar 29 '25 01:03 effect-bot