Master advanced TypeScript utility types for better type safety
# TypeScript Advanced Utility Types for Google Antigravity
Master TypeScript utility types to build robust, type-safe applications with Google Antigravity IDE.
## Built-in Utility Types
```typescript
// Partial - Make all properties optional
interface User {
id: string;
name: string;
email: string;
age: number;
}
type PartialUser = Partial<User>;
// Required - Make all properties required
type RequiredUser = Required<PartialUser>;
// Pick - Select specific properties
type UserPreview = Pick<User, "id" | "name">;
// Omit - Remove specific properties
type UserWithoutAge = Omit<User, "age">;
// Record - Create object type with specific keys
type UserRoles = Record<"admin" | "user" | "guest", User>;
// ReturnType - Get function return type
function getUser() {
return { id: "1", name: "John" };
}
type UserReturn = ReturnType<typeof getUser>;
// Parameters - Get function parameters as tuple
function createUser(name: string, age: number, email?: string) {}
type CreateUserParams = Parameters<typeof createUser>;
```
## Custom Utility Types
```typescript
// Deep Partial - Make all nested properties optional
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};
// ReadonlyDeep - Make all nested properties readonly
type ReadonlyDeep<T> = {
readonly [P in keyof T]: T[P] extends object ? ReadonlyDeep<T[P]> : T[P];
};
// RequireAtLeastOne - Require at least one property
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>> &
{ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>> }[Keys];
// Filter keys by value type
type FilterByType<T, ValueType> = {
[K in keyof T as T[K] extends ValueType ? K : never]: T[K];
};
// Getters type
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
```
## Conditional Types
```typescript
// Infer array element type
type ElementType<T> = T extends (infer E)[] ? E : T;
// Infer Promise resolved type
type Awaited<T> = T extends Promise<infer R> ? Awaited<R> : T;
// Distributive conditional types
type ToArray<T> = T extends any ? T[] : never;
type StringOrNumberArray = ToArray<string | number>;
```
## Best Practices
1. **Use built-in utilities** before creating custom ones
2. **Leverage mapped types** for transformations
3. **Apply conditional types** for complex logic
4. **Use template literal types** for string manipulation
5. **Create reusable utility types** for your domain
6. **Document complex types** with JSDoc
7. **Test types** with type assertions
Google Antigravity provides intelligent TypeScript suggestions and helps create custom utility types.This typescript prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their typescript implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For typescript projects, consider mentioning your framework version, coding style, and any specific libraries you're using.