Günlük TypeScript geliştirme için kullanışlı özel yardımcı tipler.
class=class="text-emerald-400">"text-gray">// Make specific keys optional
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
class=class="text-emerald-400">"text-gray">// Make specific keys required
type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
class=class="text-emerald-400">"text-gray">// Deep partial — all nested properties become optional
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}
class=class="text-emerald-400">"text-gray">// Extract non-nullable values from an object type
type NonNullableFields<T> = {
[P in keyof T]: NonNullable<T[P]>
}
class=class="text-emerald-400">"text-gray">// Create a type from an arrayclass=class="text-emerald-400">"text-emerald-400">'s values
const ROLES = ['adminclass=class="text-emerald-400">"text-emerald-400">', 'editorclass=class="text-emerald-400">"text-emerald-400">', 'viewerclass=class="text-emerald-400">"text-emerald-400">'] as const
type Role = (typeof ROLES)[number] class=class="text-emerald-400">"text-gray">// 'adminclass=class="text-emerald-400">"text-emerald-400">' | 'editorclass=class="text-emerald-400">"text-emerald-400">' | 'viewer'
class=class="text-emerald-400">"text-gray">// Strongly-typed Object.keys
function typedKeys<T extends object>(obj: T): (keyof T)[] {
return Object.keys(obj) as (keyof T)[]
}Bazı alanların opsiyonel olduğu formlar oluştururken PartialBy kullanın: type CreateUser = PartialBy<User, 'id' | 'createdAt'>. İç içe nesnelerdeki güncelleme işlemleri için DeepPartial kullanın.
Fikrinizi nasıl hayata geçirebileceğimizi konuşalım. İlk konseptten üretime hazır ürüne kadar — yanınızdayız.