sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.79k stars 153 forks source link

Using Static on a value from TypeSystem.Type does not work as in documentation #389

Closed Zajozor closed 1 year ago

Zajozor commented 1 year ago

Hi, thanks for the amazing library!

I'm trying to do what is here in documentation: https://github.com/sinclairzx81/typebox#types-1

type PointOptions = {} // The Type Options

type PointType = { x: number; y: number } // The Static<T> Type

const Point = TypeSystem.Type<PointType, PointOptions>(
  'Point',
  (options, value) => {
    return (
      typeof value === 'object' &&
      value !== null &&
      'x' in value && // added in addition to what was in the example to stop TS complaints
      typeof value.x === 'number' &&
      'y' in value &&  // added in addition to what was in the example to stop TS complaints
      typeof value.y === 'number'
    )
  }
)

const T = Point()

type T = Static<typeof T> // type T = { x: number, y: number }

const R = Value.Check(T, { x: 1, y: 2 }) // const R = true

But on line type T = Static<typeof T> // type T = { x: number, y: number }

I'm getting

Type 'TUnsafe<PointType>' does not satisfy the constraint 'TSchema'.
  Property '[Kind]' is missing in type 'TUnsafe<PointType>' but required in type 'TSchema'.ts(2344)

Can you please help? Is the example incorrect? It really feels like if I understood the feature correctly, it should work like this.. Is there perhaps an error in the docs?

Using "@sinclair/typebox": "0.27.8", (latest at the time of writing)

Appreciate your time 🙏🏻

Zajozor commented 1 year ago

Oh the moment I hit Submit I realized what's the issue 🙈

I've been using @fastify/type-provider-typebox and it resolved @sinclair/typebox to an older version than 0.27.8.

So for example doing

  "resolutions": {
    "@sinclair/typebox": "0.27.8"
  }

in package.json "removes" the issue. (keep in mind to check that the library you're forcing the resolution for is compatible with the force-resolved version)