fp-ts-std icon indicating copy to clipboard operation
fp-ts-std copied to clipboard

`S.omit` fails on optional properties

Open samhh opened this issue 3 years ago • 2 comments

See: b3d8ab0e52, #141

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

samhh avatar Sep 12 '22 11:09 samhh

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

JLambertazzo avatar Mar 12 '23 21:03 JLambertazzo

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

samhh avatar Apr 07 '23 11:04 samhh