metadevpro / openapi3-ts

TS Model & utils for creating and exposing OpenAPI 3.x contracts.
MIT License
485 stars 64 forks source link

fix: use Record for properties field in Schema Object #135

Open vaibhavrajsingh2001 opened 5 months ago

vaibhavrajsingh2001 commented 5 months ago

Using mapped types like { [propertyName: string]: SchemaObject | ReferenceObject } doesn't ensure the key to be of type string. Instead, TypeScript interprets the key as string | number.

image

This behaviour can be verified in the following playground

Due to this issue, if you iterate over the properties of a schema, TypeScript can't be sure that the key is of type string. Instead the key will be interpreted as of type string | number.

Using a Record allows to strictly type the key to only be of type string, which can be verified here

image

It's a known TypeScript issue https://github.com/microsoft/TypeScript/issues/48269

vaibhavrajsingh2001 commented 5 months ago

Hey @pjmolina, came across this issue while I was trying to iterate over the properties field of a SchemaObject. Would appreciate a review here 🙏.

pjmolina commented 5 months ago

I have contradicting feelings here:

To be honest, not sure if it is makes worthy. And if we are breaking uses cases for anyone else.