sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.65k stars 150 forks source link

Error when trying to use TSchema as a generic #806

Closed Qrokqt closed 4 months ago

Qrokqt commented 4 months ago

I've got a project setup using pnpm workspaces setup like so

shared
  "@sinclair/typebox": "0.32.19"

server
  "shared": "workspace:*",
  "@sinclair/typebox": "0.32.19"

in shared im exporting a schema

import { Type } from '@sinclair/typebox';
export const bar = Type.Object({});

and in the server im importing it and trying to use it

import { TSchema } from '@sinclair/typebox';
import { bar } from 'shared/schemas'
function foo<T extends TSchema>(schema: T) {}
foo(bar);

but I'm getting the error

Argument of type 'TObject<{}>' is not assignable to parameter of type 'TSchema'.
  Property '[Kind]' is missing in type 'TObject<{}>' but required in type 'TSchema'.

Is TSchema the wrong type to use or any other idea why its not working?

sinclairzx81 commented 4 months ago

@Qrokqt Hi!

Is TSchema the wrong type to use or any other idea why its not working?

The code you have there looks correct. However, I'm not a familiar with pnpm, so can't provide must assistance there. Some things you can try though.

1) Ensure all projects are configured as strict (tsconfig.json) 2) Ensure all projects are configured as either module: CommonJS, or module: NodeNext (tsconfig.json) (these should be the same across projects in your monorepo as TB provides both ESM and CJS versions which are incompatible with each other) 3) Ensure you only have one version of TypeBox installed.

Give the above a try. If this doesn't solve the issue, if you're able to setup a simple GH reproduction project, I may be able to provide a bit more assistance.

Cheers S

Qrokqt commented 4 months ago

Thanks, in trying to create a repro I found that I had missed "type": "module" in the shared workspace. Once I added that it stopped throwing the error.