ts-expand-type
This repo provides a TS Server plugin which amends quick info to include the fully "expanded" type. "Expanded" means that object intersections are recursively merged, and nested type aliases are resolved.
Additionally, it exposes utilities for expanding types generated by the TypeScript Compiler. For more details, see Usage as a Library
Installing
To install locally, first install the package as a dev dependency (npm i -D ts-expand-type
), and then add the following to your tsconfig.json
:
{
"compilerOptions": {
// ...
"plugins": [
{
"name": "ts-expand-type",
// ...config options
}
]
// ...
}
}
Some IDEs may support global plugins, since this feature is supported by tsserver
. Consult your IDE's documentation for more info.
Note that if you are using VSCode, an extension is planned to integrate this feature standalone. Check back later for more info.
Config
Configuration options are set in the plugins
options - see above. Available configuration options:
multilineObjectLiterals
: Whether to force all object literals to be rendered across multiple lines. Defaults totrue
includeOriginal
: Whether to include the original quick info or override it with type info. Defaults tofalse
typePrefix
: Whether to prefix the type info with the text(type)
. Defaults totrue
ifincludeOriginal
is set, andfalse
otherwise.
Planned Features
Planned features:
- Support for mapped types
- Union merging (#1)
- Support for function types
Usage as a Library
This package exposes some API enpoints for acessing certain behavior. Specifically, you can import from ts-expand-type/dist/merge
and ts-expand-type/dist/util
.
The function you are probably most interested in is recursiveMergeIntersection
from merge.js
, which takes a type and "expands" it in the way described above. This should then be good to print using typeChecker.typeToString()
.
Testing
Write test cases in tests/cases
. Running,
yarn test
Will generate "baselines," which test every typed node in the file, for each test case. It will then compare generated baselines to those in tests/baselines/reference
. Any which fail will go into the tests/baselines/local
folder, where you can inspect/diff them. To accept new baselines, run,
yarn baseline-accept
If you make a PR adding new features, please write at least one test case for that feature.
Building
First install deps with yarn install
. To build,
yarn build