samhh / fp-ts-std

The missing pseudo-standard library for fp-ts.
https://samhh.github.io/fp-ts-std/
MIT License
207 stars 27 forks source link

`S.omit` fails on optional properties #144

Open samhh opened 2 years ago

samhh commented 2 years ago

See: b3d8ab0e52, #141

Notably this isn't an issue with S.omitFrom.

JLambertazzo commented 1 year ago

This could be achieved by replacing the use of Record in this module to something that looks more like a Struct. I've been using the type definitions to test some things with

type Struct = {[key: string]: unknown}
type StructWithKey<K extends string> = {[key in K]?: unknown} & Struct

Using these types we can define an omit function where the generic struct A is defined by the generic key K

const omit = <K extends string>(keys:readonly  K[]) => <A extends StructWithKey<K>>(struct: A): Omit<A, K> => {
  const y = {...struct}

  for (const key of keys) {
    delete y[key]
  }

  return y
}

This successfully omits specified fields and doesn't remove typing from other parameters after omit

samhh commented 1 year ago

@JLambertazzo Would that be backwards-compatible with uses of Record?