Skip to content

Instantly share code, notes, and snippets.

@danvk
Created June 20, 2022 23:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvk/8be29b26a814e677245aec52f0788d4c to your computer and use it in GitHub Desktop.
Save danvk/8be29b26a814e677245aec52f0788d4c to your computer and use it in GitHub Desktop.
Pruning a transitive type dependency for TypeScript

Pruning a transitive type dependency for TypeScript

See Stack Overflow: How can I completely hide an irrelevant directory of type declarations from TypeScript?.

This is mostly interesting as a template for using paths to prune the set of files considered by tsc when building a project.

Solution tsconfig.json:

{
  "compilerOptions": {
    "paths": {
      "@pgtyped/query": [ "./declarations/pgtyped-query.d.ts" ],
      "antlr4ts" : [ "./declarations/antlr4ts.d.ts" ],
      "antlr4ts/RecognitionException": [ "./declarations/antlr4ts.d.ts" ]
    }
  }
}

The contents of node_modules/@pgtyped/query/lib/index.d.ts as distributed on npm look like this:

export { getTypes, startup, IParseError } from './actions';
export { ParamTransform, IQueryParameters, IInterpolatedQuery, } from './preprocessor';
export { processTSQueryAST } from './preprocessor-ts';
export { processSQLQueryIR } from './preprocessor-sql';
export { AsyncQueue } from '@pgtyped/wire';
export { default as parseTypeScriptFile, TSQueryAST, } from './loader/typescript';
export { default as parseSQLFile, SQLQueryAST, SQLQueryIR, prettyPrintEvents, queryASTToIR, } from './loader/sql';
export { default as sql, TaggedQuery, PreparedQuery } from './tag';

All I care about is the tag exports (sql and TaggedQuery). So I swapped in a version in declarations that only exports these (see other file in this gist).

Then I have a stub declarations file for the two antlr4ts modules that get imported by .d.ts files under tag.d.ts (also see attached).

The net result in my project is that antlr4ts is removed from the list of files considered by tsc.

Before

$ tsc --noEmit --listFiles | wc -l
3575
$ tsc --noEmit --listFiles | grep antlr4ts | wc -l
111

After

$ tsc --noEmit --listFiles | wc -l
3456
$ tsc --noEmit --listFiles | grep antlr4ts | wc -l
1

So 119 files removed from compilation, and the only remaining vestige of antlr4ts is my own declarations file. Hooray!

declare module 'antlr4ts';
declare module 'antlr4ts/RecognitionException';
declare module '@pgtyped/query' {
export {default as sql, TaggedQuery, PreparedQuery} from '@pgtyped/query/lib/tag';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment