gatsby-typescript
gatsby-typescript copied to clipboard
Types not generated for all files, ambiguous build error
It worked once, then decided to quit on me. I'm using gatsby-plugin-graphql-codegen
with the default config, not gatsby-plugin-ts
. It's very possible I've just done something wrong; I'm new to gatsby and graphql both, but this error message isn't very useful:
warn [gatsby-plugin-graphql-codegen] Cannot read property 'buildError' of undefined
info [gatsby-plugin-graphql-codegen] definition for queries of schema default-gatsby-schema has been updated at
Types are being generated, but missing for at least one file:
import React from 'react';
import { graphql } from 'gatsby';
import { QueryBlogPostBySlug } from '../../graphql-types'; // type error; module graphql-types has no exported member QueryBlogPostBySlug
interface BlogPostProps {
data: QueryBlogPostBySlug;
}
const BlogPost: React.FC<BlogPostProps> = props => {
// ...
};
export const query = graphql`
query BlogPostBySlug($slug: String!) {
site {
siteMetadata {
title
author
}
}
allButterPost(filter: { slug: { eq: $slug } }) {
edges {
node {
id
body
seo_title
date
categories {
name
}
}
}
}
}
`;
export default BlogPost;
My query looks fine and runs fine, so I'm not sure what I'm missing. Repo is here.
Did some triangulation. Build seems to be choking on the fact that I have a file src/global.d.ts
,
declare const __PATH_PREFIX__: string;
declare module 'typography-theme-moraga' {
import { TypographyOptions } from 'typography';
const Theme: TypographyOptions;
export = Theme;
}
declare module 'typography-theme-judah' {
import { TypographyOptions } from 'typography';
const Theme: TypographyOptions;
export = Theme;
}
As soon as I remove the file, the types are generated as expected.
I have a similar setup with type augments but my config is slightly different.
Can you try:
- moving
src/global.d.ts
totypes/global.d.ts
- add this to your
tsconfig.json
{
// ...
"compilerOptions": {
// ...
"typeRoots": [
"node_modules/@types",
"types"
],
}
// ...
}
Let me know if that does something.
Thanks for opening this issue @OfficerHalf, putting a global.d.ts in src dir is reasonable & shouldn't cause issue. Could you try @ricokahler's workaround for now? My apologies for the inconvenience.
It's possible that this is an issue with the upstream lib, I'll take a look.
Same issue here. Quite a pain. Trying to move my types out of src
per the suggestion seems to lead to a whole slew of other issues in my monorepo.
I spent a bit of time debugging and it's definitely an upstream lib — graphql-toolkit
. The ability to include and exclude files by patterns is one option. If ambient types are universally the issue then .d.ts
files could be excluded as a rule.
Ah, I just found options.documentPaths
. By excluding .d.ts
files there I was able to solve my problem:
documentPaths: [
'./src/**/!(*.d).{js,jsx,ts,tsx}',
'./.cache/fragments/*.js',
'./node_modules/gatsby-*/**/*.js'
]