ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Cannot find type from `declare global`

Open imtoo opened this issue 2 years ago • 0 comments

Hi, I'm trying to generate a schema from type that includes a global type.

types/generated/graphql.d.ts

export {}

declare global {
  export type TermType = 'DAILY' | 'WEEKLY'
}

src/types.ts

export interface Events {
  Term: {
    duration: number
    term: TermType
  }
}

src/generate-json-schema.js

const tsj = require('ts-json-schema-generator')
const fs = require('fs')
const path = require('path')

const config = {
  path: path.resolve(__dirname, 'types.ts'),
  tsconfig: path.resolve('tsconfig.json'),
  type: 'Events',
  expose: 'none',
}

const outputPath = path.resolve(__dirname, 'events.schema.json')
const schema = tsj.createGenerator(config).createSchema(config.type)
const schemaString = JSON.stringify(schema)
fs.writeFile(outputPath, schemaString, (err) => {
  if (err) throw err
})

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "sourceMap": true,
    "inlineSources": true,
    "sourceRoot": "src",
    "module": "esnext",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": "src",
    "typeRoots": ["./node_modules/@types", "./types"],
    "types": ["jest", "node"],
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src", "types"]
}

I'm getting this error: Screenshot 2022-05-19 at 13 04 47

The project with global types compiles just fine, IDE is not complaining as well. I'm just not able to generate json-schema with global type, if I remove that global type it works fine.

Is that a limitation of TS compiler or AST parser or am I doing something wrong?

imtoo avatar May 19 '22 11:05 imtoo