Guardz Generator VS Code Extension
A VS Code extension that integrates with the guardz-generator
package to automatically generate TypeScript type guards from your existing TypeScript interfaces and types.
Features
- Generate Type Guards: Generate type guards for all types in the current TypeScript/JavaScript file
- Generate from Selection: Select specific code and generate type guards for only those types
- Context Menu Integration: Right-click in TypeScript/JavaScript files to access the generator
- Command Palette Support: Use the command palette to access all features
Commands
guardz-generator.generateTypeGuards
Generates type guards for all types defined in the current file. The generated code will be opened in a new tab.
guardz-generator.generateFromSelection
Generates type guards for the selected code only. The generated code will replace the selection.
Usage
Method 1: Generate from entire file
- Open a TypeScript or JavaScript file
- Press
Ctrl+Shift+P
(or Cmd+Shift+P
on Mac) to open the command palette
- Type "Generate Type Guards" and select the command
- The generated type guards will open in a new tab
Method 2: Generate from selection
- Select the TypeScript/JavaScript code you want to generate type guards for
- Right-click and select "Generate Type Guards from Selection"
- The generated type guards will replace your selection
Method 3: Command palette
- Press
Ctrl+Shift+P
(or Cmd+Shift+P
on Mac)
- Type "Guardz Generator" to see all available commands
- Select the desired command
Example
Given this TypeScript interface:
interface User {
id: number;
name: string;
email: string;
isActive: boolean;
profile?: {
avatar: string;
bio: string;
};
}
The extension will generate type guards like:
import { isString, isNumber, isBoolean, isObject, isOptional } from 'guardz';
export const isUser = (value: unknown): value is User => {
return isObject(value) &&
isNumber((value as any).id) &&
isString((value as any).name) &&
isString((value as any).email) &&
isBoolean((value as any).isActive) &&
isOptional((value as any).profile, isUserProfile);
};
export const isUserProfile = (value: unknown): value is User['profile'] => {
return isObject(value) &&
isString((value as any).avatar) &&
isString((value as any).bio);
};
Requirements
- VS Code 1.102.0 or higher
- TypeScript or JavaScript files
Installation
- Clone this repository
- Run
pnpm install
to install dependencies
- Run
pnpm run compile
to build the extension
- Press
F5
in VS Code to launch the extension in a new Extension Development Host window
Development
pnpm run compile
- Compile the TypeScript code
pnpm run watch
- Watch for changes and recompile
pnpm run test
- Run tests
License
MIT