d4rekanguok/gatsby-typescript

[codegen] improve importing types from generated file

Opened this issue ยท 7 comments

This way user can just import the file directly

import { MyQuery } from 'gatsby-ts'

Instead of the ../../.. they're doing now.

Also avoid the confusion shown in #85

FYI: This will never work with Yarn 2

And when my plugin had used this approach by default before, TypeScript did detect changes in node_modules rather slower than in src. (in VSCode, not sure it's IDE dependent)

ha, forgot about that.. thanks for the reminder! @cometkim

@d4rekanguok I haven't confirmed if this will work or not but here's an idea:

  1. Instead of writing to node_modules we can write to gatsby .cache.
  2. We can utilize the tsconfig.extends option and advise users to extend a pre-made configuration that will include this cache location

As far as I know, i think it's possible to create a packages for tsconfigs.


So I think users would have tsconfig files that would look like this:

{
  "extends": "gatsby-ts/tsconfig",
  "compilerOptions": {
    // ...
  },
  // ...
}

And our base config would include a typeRoots configuration that will look in the .cache folder.

@ricokahler That's a nice idea, I'll give it a try locally!

@d4rekanguok you should checkout the typegen plugin. Since v2, I've moved generated type definitions into a specific namespace declaration, TypeScript will understand the definition without importing module.

NexusJS also has a similar approach with this and they have great DX with code generation.

Thanks @cometkim, I'll check it out v2 & also nexusJS!