mylesmmurphy / prettify-ts

Prettify TypeScript: Better Type Previews
https://marketplace.visualstudio.com/items?itemName=MylesMurphy.prettify-ts
MIT License
181 stars 6 forks source link

feat: use root tsconfig.json if available #3

Closed AlfonzAlfonz closed 6 months ago

AlfonzAlfonz commented 6 months ago

Right now prettify-ts may resolve some types incorrectly because it is missing path to tsconfig.json file. This is issue for example when allowJs and checkJs flags are enabled in the tsconfig.json and project relies on importing js files. This can be fixed in prettifyType function when ts-morph Project is instanciated:

  const project = new Project({
    manipulationSettings: { indentationText: IndentationText.TwoSpaces },
    tsConfigFilePath: '...'
  })

This is implementation of trying to find tsconfig.json in the root workspace folder of the current file. This may not work perfectly in monorepos, where multiple tsconfig.json may be present in subfolder, but I'd say that this is good enough and this implementation only used a single filesystem call to test whether the tsconfig file exists. Alternatively this can be implemented by checking the file's parent directories to find existing tsconfig. This would result in multiple fs calls because the config would be usually found near the root.