Comprehensive TypeScript guidance covering compiler configuration, advanced types, utility types, type guards, strict mode workflows, and documentation patterns; use when configuring tsconfig, designing complex generics, making illegal states unrepresentable, fixing type errors, or writing testable and maintainable type-safe APIs.
Overall
score
99%
Does it follow best practices?
Validation for skill structure
Makes all properties in T optional.
interface User {
id: string;
name: string;
email: string;
age: number;
}
type PartialUser = Partial<User>;
// {
// id?: string;
// name?: string;
// email?: string;
// age?: number;
// }
function updateUser(id: string, updates: Partial<User>) {
// Apply only the provided fields
}
updateUser('123', { name: 'John' }); // ✅ Only name provided
updateUser('123', { age: 30, email: 'john@example.com' }); // ✅type Partial<T> = {
[P in keyof T]?: T[P];
};API Updates (PATCH)
async function patchUser(id: string, updates: Partial<User>) {
return fetch(`/api/users/${id}`, {
method: 'PATCH',
body: JSON.stringify(updates)
});
}Form State
interface FormData {
username: string;
password: string;
email: string;
}
function FormComponent() {
const [formData, setFormData] = useState<Partial<FormData>>({});
const updateField = (field: keyof FormData, value: string) => {
setFormData(prev => ({ ...prev, [field]: value }));
};
}Default Configuration
interface Config {
host: string;
port: number;
timeout: number;
retries: number;
}
const defaultConfig: Config = {
host: 'localhost',
port: 3000,
timeout: 5000,
retries: 3
};
function createConfig(overrides: Partial<Config>): Config {
return { ...defaultConfig, ...overrides };
}
const config = createConfig({ port: 8080 }); // ✅ Only override portMakes all properties in T required.
interface User {
id?: string;
name?: string;
email?: string;
}
type CompleteUser = Required<User>;
// {
// id: string;
// name: string;
// email: string;
// }
function saveUser(user: Required<User>) {
// All fields guaranteed to exist
console.log(user.id.toUpperCase()); // ✅ No undefined check needed
}type Required<T> = {
[P in keyof T]-?: T[P]; // -? removes optional modifier
};Database Operations
interface DraftPost {
title?: string;
content?: string;
authorId?: string;
publishedAt?: Date;
}
// Database requires all fields
function insertPost(post: Required<DraftPost>) {
db.posts.insert(post);
}
// Validation before saving
function validateAndSave(draft: DraftPost) {
if (isComplete(draft)) {
insertPost(draft as Required<DraftPost>);
}
}
function isComplete(draft: DraftPost): draft is Required<DraftPost> {
return !!(draft.title && draft.content && draft.authorId);
}API Response Validation
interface ApiUser {
id?: string;
name?: string;
email?: string;
}
function validateUser(user: ApiUser): Required<ApiUser> {
if (!user.id || !user.name || !user.email) {
throw new Error('Invalid user data');
}
return user as Required<ApiUser>;
}type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
interface User {
id: string;
name: string;
email: string;
age: number;
}
// Make only age optional
type UserWithOptionalAge = PartialBy<User, 'age'>;
// {
// id: string;
// name: string;
// email: string;
// age?: number;
// }type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
interface DraftUser {
id?: string;
name?: string;
email?: string;
}
// Require only name and email
type MinimalUser = RequiredBy<DraftUser, 'name' | 'email'>;
// {
// id?: string;
// name: string;
// email: string;
// }For nested objects:
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};
type DeepRequired<T> = {
[P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
};
interface NestedConfig {
server: {
host: string;
port: number;
ssl: {
enabled: boolean;
cert: string;
};
};
}
type PartialConfig = DeepPartial<NestedConfig>;
// All nested properties become optional
const config: PartialConfig = {
server: {
port: 8080
// ssl not required, host not required
}
};Install with Tessl CLI
npx tessl i pantheon-ai/typescript-advancedreferences