patch-ts is a command-line interface (CLI) designed to enhance the TypeScript compiler by patching its the checker.ts file which creates the types. All changes can be found in the templates.ts file.
Input:
module.exports = {
Email: {
fn: (str) => str.includes("@") ? str : "NEVER",
type: "boolean",
},
};Output:
type Email<S> = intrinsic;
type IsEmail = Email<'Hi'>;
// ^? false- Patch TypeScript: Apply modifications to enhance the TypeScript compiler with the ability to parse custom
intrinsictypes. - Create
.d.tsfile from intrinsics Use the intrinsics.js file to create the corresponding.d.tsfiles. - Revert Changes: Restore the original state of patched TypeScript files.
- Node.js (LTS version recommended)
- TypeScript installed globally or in your project
Install it using npm:
npm i -g patch-ts-
Figure out the path of the TypeScript version you are currently using. For Example:
patch-ts show-lib > /home/wrz/node/v22.2.0/lib/node_modules/typescript/libThis uses the currently installed
tscto check the library path. -
Make sure that your IDE (VSCode for example) points to the same TypeScript compiler. Check it by running the command-palette command:
Select TypeScript Version
-
If the paths do not align with the one from
patch-ts libyou need to align it by setting it in your settings json:{ "typescript.tsdk": "/home/wrz/node/v22.2.0/lib/node_modules/typescript/lib" } -
Use the patch command to patch your TypeScript installation with an intrinsics file by using the environment variable "PATCH_TS". First you need to set the environment variable for example in your
.bashrcor.zshrc.export PATCH_TS=/path/to/your/intrinsics.jsAnd then patch it:
patch-ts patch --useEnv PATCH_TS
-
Now you can add your own intrinsic types to the
intrinsics.jsfile. -
RESTART: To apply the changes made to the
intrinsics.jsfile you have to restart your ts-server usingRestart TS Server. -
If you want to, you can now also create the types from the intrinsics file:
patch-ts create-dts -i ./intrinsics.js -o ./my.d.ts
To patch TypeScript compiler files:
patch-ts patch [options]-f, --force: Force the patch operation, even if already patched.-l, --libPath <value>: Specify the path to the TypeScript library. By default it uses the current libtscpoints to.-ue, --useEnv: If set, it will use the provided environment variable and the path behind it to determine the path to theintrinsics.jsfile. Needs a restart of VSCode to find the new environment variable.-b, --bundled: The file which will be bundled into the compiler. Will not be updated but only the current version is copied into the compiler.
### Show Lib
Use this command to get the lib which will get patched when calling patch-ts.
patch-ts show-libpatch-ts create-dts -i /path/to/intrisincs.js -o ./myfile.d.tsWill use the file from the input path to create the TypeScript types file.
To revert any changes made by the patch:
patch-ts revertThis command will revert the TypeScript files (typescript.js and tsc.js) to their original state before any patches applied by patch-ts.
Patching with a custom lib path and using the PATCH_TS environment variable to figure out where the patch-file is. Will update when the file at PATCH_TS changes and the user restarts the ts server. Also forcing an update.
patch-ts patch --useEnv PATCH_TS --path /Users/wrz/IdeaProjects/TypeScript/built/local --forceBundle a intrinsicts file into the compiler. Is only bundled ONCE.
patch-ts patch --bundled /path/to/custom-intrinsics.jsThis command will use the custom-intrinsics.js file for patching the typescript compiler ONCE. It does not update when the file changes.
The JavaScript file has to have a single module.exports which exports a function which looks like that:
module.exports = function () {
return {
Email: {
fn: (str) => str.includes("@" ? str : "NEVER"),
},
Length: {
fn: (str) => str.length,
type: "number",
},
Add: {
fn: (first, second) => first + second,
type: "number",
},
IsNumber: {
fn: nr => nr === +nr,
type: "boolean"
}
};
};The fn property is always needed. It maps the generics which we pass to the generic type and transform it. By default it will always return the result as a string representation. We can specify the type property as either number, string or boolean to transform the result into the corresponding type.
When "NEVER" is returned, it will be converted to the never type.
This will enable us to create these types:
type Email<S> = intrinsic;
type Length<S> = intrinsic;
type Add<F, S> = intrinsic;
type WrongEmail = Email<"ab">;
// ^? never
type CorrectEmail = Email<"a@b">;
// ^? "a@b"
type TextLength = Length<"hi">;
// ^? 2
type NumberCheck = IsNumber<1>;
// ^? trueContributions to patch-ts are welcome! Please read the contributing guidelines in the repository for more information on how to submit pull requests.
This project is licensed under the MIT License.
For support, submit an issue in the GitHub repository or contact the project maintainers.