yaml icon indicating copy to clipboard operation
yaml copied to clipboard

FR: Change YAMLMap to support non record types

Open inlined opened this issue 1 year ago • 1 comments
trafficstars

Is your feature request related to a problem? Please describe. YAML documents often have a structure with known key types turning into known value types. The current YAMLMap type is based on a Record type which means any key of the input object maps to any value of the input object.

Describe the solution you'd like Change the template type for YAMLMap from YAMLMap<K = unknown, V = unknown> to YAMLMap<T extends Record<unknown, unknown>> and propagate types, E.g. YAMLMap<T>::get<S extends keyof T>(key: S, keepScalar?: false): T[S]

This will both prevent typos in "get" calls, and remove unnecessary undefined checks if the schema ensures a value is present.

Describe alternatives you've considered Keep losing type information as I navigate the document

Additional context Chose this library because of its support for incremental changes to YAML, which will let me modify config files with minimal diffs. This was clearly a lot of hard work to expose this library and I appreciate what you've done so far 😄

inlined avatar Apr 09 '24 15:04 inlined

Sounds like a pretty good idea. I'd be happy to consider a PR for this.

Note that the current compiled types are tested to work in TS 3.9 or later; sticking to that is not a hard limit, we can increase the minimum in a minor version update.

eemeli avatar Apr 09 '24 16:04 eemeli