TypeScript - Utilities

Record

type RegisterFormInput = {
name: string
email: string
password: string
address: string
phoneNumber: string
username: string
}

can be written like this:

enum RegisterFormField {
name,
email,
password,
address,
phoneNumber,
username,
}
type RegisterFormInput = Record<keyof typeof RegisterFormField, string>

or :

enum RegisterFormField {
name,
email,
password,
address,
phoneNumber,
username,
}
type RegisterFormFieldType = keyof typeof RegisterFormField
type RegisterFormInput = {
[key in RegisterFormFieldType]: string
}
/** or */
type RegisterFormInput = Record<RegisterFormFieldType, string>

other examples:

type CardSpecs = {
name: string
ID: number
isPassive: boolean
}
type CardRarity = 'common' | 'rare' | 'epic'
/**
can be written like this:
enum CardRarityName {
common,
rare,
epic
}
type CardRarity = keyof typeof CardRarityName;
*/
type CardCollection = Record<CardRarity, CardSpecs>
/**
result:
type CardCollection = {
common: {
name: string;
ID: number;
isPassive: boolean;
};
rare: {
name: string;
ID: number;
isPassive: boolean;
};
epic: {
name: string;
ID: number;
isPassive: boolean
}
}
*/

references:

Partial

type RegisterFormInput = {
name: string
email: string
password: string
address: string
phoneNumber: string
username: string
}
type OptionalRegisterFormInput = {
name?: string
email?: string
password?: string
address?: string
phoneNumber?: string
username?: string
}

can be written like this:

type RegisterFormInput = {
name: string
email: string
password: string
address: string
phoneNumber: string
username: string
}
type OptionalRegisterFormInput = Partial<RegisterFormInput>

reference: TypeScript Docs

other references: