sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.56k stars 148 forks source link

Typescript doesn't suggests the props using Pick or Omit #874

Closed yanmifeakeju closed 1 month ago

yanmifeakeju commented 1 month ago

I've noticed that when using TypeScript in our project, the IDE doesn't suggest props when utilizing the Pick or Omit methods. I have a feeling that this functionality used to be available before.

Any insights on why this feature is no longer working or if I may be mistaken about its previous availability

sinclairzx81 commented 1 month ago

@yanmifeakeju Hi, sorry for the delay in reply.

TypeBox previously inferred Pick/Omit and Index property keys, however it was removed on 0.32.x as the inference required to obtain the keys was proving very expensive (as TB needed to either Static or derive keys during composition (which was a problem for very deeply nested / computed types)), also the exact property key values resulted in a few issues when using Pick/Omit/Index in generic functions (where keys are passed as generic arguments)

As of 0.32.0, TypeBox allows passing any keys, however you should check the return value of Omit/Pick and Index to ensure the operation produced the expected type.

import { Type } from '@sinclair/typebox'

const T = Type.Object({
  x: Type.Number(),
  y: Type.Number()
})

// As of 0.32.x...

const A = Type.Pick(T, ['z'])  // TObject<{}>

const B = Type.Omit(T, ['z'])  // TObject<{ a: TNumber, b: TNumber }>

const C = Type.Index(T, ['z']) // TNever

Hope this provides some insight into the change. Will close off this issue for now, but if you have any additional questions on the above, feel free to ping on this thread.

All the best S