Coder Social home page Coder Social logo

Comments (16)

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024 3

@AaronPorts Have submitted a PR to have this resolved.

Given the holiday period, it may take some time to merge. If you can get by with the CommonJS configuration in the interim, that should hold until things get merged downstream, alternatively, just keep on with 0.31.0.

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024 2

@SuperKXT @AaronPorts Hiya,

Looks like the Type Provider updates were published this morning. You can install the following which should solve ESM configuration issues.

@fastify/[email protected]
@sinclair/typebox@0.32.4

Have tested locally and things should be ok, but if you run into any issues, please let me know (I'm currently in the process of chasing down and documenting ESM issues, so any issues reported are very helpful)

Cheers!

from typebox.

dwickern avatar dwickern commented on June 5, 2024 1

👍 Everything works on my end (using CJS)

from typebox.

SuperKXT avatar SuperKXT commented on June 5, 2024 1

Would you like to submit a PR to the Fastify Type Provider project to bump the peer dependency range?

Sure!

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024 1

@AaronPorts Hi

Have just re-tested and things should be working ok on @fastify/type-provider-typebox: 3.6.0. I can only really recommend trying to clean out node_modules and reinstalling to ensure there's no transient older versions of TypeBox installed. My hunch is that TS may be having difficulty resolving for multiple installed versions of TypeBox, but outside of this, I don't have much additional information to go on.

Edit: Sorry, I have been able to reproduce this on 3.6.0. Will investigate.

from typebox.

SuperKXT avatar SuperKXT commented on June 5, 2024 1

Heroic! Thank you! Will revisit this discussion if I encounter any other issues.

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024

@dwickern Hi, thank you for taking the time to test!

Sadly, didn't get a lot of community testing over the week as I'd hoped for (I guessing it's quite rare for people to check issue thread notices). Still, will be pushing out this revision this afternoon. Will be tracking any significant issues over the next few days, but hoping for the best! :)

Thanks again
S

from typebox.

SuperKXT avatar SuperKXT commented on June 5, 2024

Upgrading to 0.32.3 broke @fastify/type-provider-typebox for me.
The generics don't seem to work properly and the types all default to unkown.

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024

@SuperKXT Hi,

The @fastify/type-provider-typebox package was tested prior to release. It should be working fine (as the provider inference hasn't changed from 0.31.0 to 0.32.0). See link below where the provider is inferring types only available on 0.32.0.

TypeScript Link Here

import Fastify from 'fastify'
import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox'

const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>()

fastify.get('/', {
  schema: {
    // Mapped Types Only Available in Revision 0.32.0
    body: Type.Mapped(Type.TemplateLiteral('b${0|1}${0|1}${0|1}${0|1}'), K => Type.String())
  }
}, (req) => {
  req.body.b0000
  req.body.b0001
  req.body.b0010
  req.body.b0011
  // ..
  req.body.b1111
  
})

You could try delete the node_modules directory and install the packages. Also, just ensure you've only got 0.32.0 installed. Let me know if this helps.

Cheers
S

from typebox.

SuperKXT avatar SuperKXT commented on June 5, 2024

Thank you for the quick response!

I have narrowed that the issue only happens if I import Type from typebox directly but does not happen if I import from @fastify/type-provider-typebox.

The issue does not happen in the playground, but I've created a minimal repro of the issue here.

Things I've tried:

  • Remove the lock file and node_modules and reinstall.
  • Use npm instead of pnpm.

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024

@SuperKXT Hiya,

I wondering if this might be to do with the type provider requiring the updated peerDependency (which is currently locked at <= 0.31.0). This range is usually updated after a TB minor revision, but has yet to be done for 0.32.0.

https://github.com/fastify/fastify-type-provider-typebox/blob/main/package.json#L8

Running on the TSP, things still seem to resolve correctly.

TypeScript Link Here

Would you like to submit a PR to the Fastify Type Provider project to bump the peer dependency range?

from typebox.

AaronPorts avatar AaronPorts commented on June 5, 2024

Still having this issue with typebox: 0.32.4 and @fastify/type-provider-typebox: 3.6.0.

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024

@SuperKXT @AaronPorts

Ok, I've been able to resolve the issue by updating tsconfig.json to the following.

{
   "module": "CommonJS",
   "moduleResolution": "Node10",
   "verbatimModuleSyntax": false,
}

I think the problem here is that TypeBox 0.32.0 has begun support for both CJS and ESM, however the Type Provider is CJS only.

What I think is happening is that TS is importing CJS types from the Provider (which works), but when importing Type from @sinclair/typebox directly AND configuring a project for module: NodeNext, this causes TS to import the ESM definitions from TypeBox (not the CJS definitions used as used the Provider), causing inference errors.

The above tsconfig.json update should ensure that module resolution is kept to CJS only (inline with the Provider). Ill have a look for some other alternative (and hopefully simpler) solutions (Unfortunately, supporting ESM has not been without it's challenges).

Let me know if this helps

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024

So, taking a deeper look into this, I don't think there's much that can be done TypeBox side to make things easier here, other than dropping support for ESM which is not really a viable option (as I had hoped to eventually drop support for CJS (one day)).

For now you will need to configure your projects for module resolution Node10 and module CommonJS. As below.

{
   "module": "CommonJS",
   "moduleResolution": "Node10",
   "verbatimModuleSyntax": false,
}

One possible solution outside of TypeBox might be to update the Type Provider to support both ESM and CJS (as TypeBox does), which would allow for both ESM and CJS TS configuration, however it's a bit of work to do this (as the tooling for dual publishing is quite bad), and any updates made to the Provider would likely need to pass through Fastify's TypeScript team, which given the nature of the issue, I would expect a PR to need a really good review and team consensus reached on how best to dual publish (I spent a few weeks working out the TB build, things are very complex). I haven't the bandwidth for this currently, but could potentially look at things semi early next year.

@AaronPorts Again, keep me posted if these configurations resolve things your side.

from typebox.

AaronPorts avatar AaronPorts commented on June 5, 2024

@sinclairzx81 Yes, it resolves the issue, but I have some packages (like got for ex.) that require esm. So this workaround is not drop in replacement in my case.

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 5, 2024

@AaronPorts Thanks for getting back

Hmmm, Not too sure what could be done TypeBox side as it "should" be configured correctly. The following links show the resolution TS uses for various module configurations.

TypeBox Resolution

image

TypeProvider Resolution

image

As you can see, the Provider is resolving CJS despite configurations for Node16 (ESM). This would suggest the Provider does need updating to support ESM resolution for Node16. If this was done, it should resolve this issue, however it's likely going to require the Provider to dual publish (which unfortunately is less trivial than it should be)

Would you like to draft an issue on the Type Provider package?

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.