Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when trying to use TSchema as a generic #806

Closed
Qrokqt opened this issue Mar 24, 2024 · 2 comments
Closed

Error when trying to use TSchema as a generic #806

Qrokqt opened this issue Mar 24, 2024 · 2 comments

Comments

@Qrokqt
Copy link

Qrokqt commented Mar 24, 2024

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
Copy link
Owner

@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
Copy link
Author

Qrokqt commented Mar 24, 2024

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.

@Qrokqt Qrokqt closed this as completed Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants