d4rekanguok / gatsby-typescript

Alternative typescript support plugin for Gatsbyjs. Aims to make using typescript in Gatsby as painless as possible
MIT License
122 stars 18 forks source link

[codegen] improve importing types from generated file #86

Open d4rekanguok opened 4 years ago

d4rekanguok commented 4 years ago

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

cometkim commented 4 years ago

FYI: This will never work with Yarn 2

cometkim commented 4 years ago

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)

d4rekanguok commented 4 years ago

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

ricokahler commented 4 years ago

@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.

d4rekanguok commented 4 years ago

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

cometkim commented 4 years ago

@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.

d4rekanguok commented 4 years ago

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