I have generated (from GraphQL Codegen and my API) a TypeScript types modules file and want to add all of its types to my project, application-wide (globally). The types are in src/generated/generatedTypes.ts which also has an export {}; line (making it a module).
tsconfig.ts defines:
"include": [ "src/generated", // bring in my generated types "**/*.ts", // bring in the rest of the project's files The first include directive tells TypeScript to load all the types from my src/generated folder (including generatedTypes.ts). However, inside my .ts files I still get "Cannot find name SomeType" errors; the types aren't there. How can I make the types in generatedTypes.ts available to the rest of my project?
Full tsconfig.ts file:
{ "ts-node": { "require": ["tsconfig-paths/register"] }, "compilerOptions": { "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "baseUrl": "./", "checkJs": true, "skipLibCheck": true, "strict": false, "forceConsistentCasingInFileNames": true, "noEmit": true, "incremental": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "paths": { "app/*": ["./app/*"], "pages/*": ["./pages/*"], "src/*": ["./src/*"] }, "plugins": [ { "name": "next" } ], "strictNullChecks": true, "target": "es2017", // "types": ["node"], // "typeRoots": ["./node_modules/@types", "./src/generated"] }, "include": [ "src/generated", "next-env.d.ts", ".next/types/**/*.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", "postgraphile.cjs", ], "exclude": ["node_modules", "migrations/**"], "skipLibCheck": false, // "skipLibCheck": true /* Skip type checking all .d.ts files. */ } The types in generatedTypes.ts aren't important but the file looks like:
export {}; type System = GQLNode & { __typename?: 'System'; createdAt?: Maybe<Scalars['Datetime']['output']>; // lots more definition }; // lots more types There is no global declaration in the file because I didn't think it was necessary. The end of the file is just:
type SignupMutation = { __typename?: 'Mutation'; registerUser?: { __typename?: 'RegisterUserPayload'; jwtToken?: any | null; } | null; };
export {}. That would make TypeScript view the file as a module rather than a script. Maybe this is also what yournoExport: trueconfiguration change does.declare global, which was my issue)..gqlfile, yourpackage.jsonincluding dependencies and the scripts to use to run codegen, etc. I'm not necessarily implying I agree with this closure, just trying to give you some insight for the future.