Loading...
Loading...
Apply, review, and explain TypeScript variable conventions. Use automatically for tasks involving variable declarations, const assertions, enum alternatives, literal-union state modelling, boolean flags, or choosing between null and undefined.
npx skill4agent add mkosir/typescript-style-guide typescript-variablestypescript-discriminated-unionsas constas constnumberstring// ❌ Avoid
const FOO_LOCATION = { x: 50, y: 130 }; // Type { x: number; y: number; }
FOO_LOCATION.x = 10;
// ✅ Use
const FOO_LOCATION = { x: 50, y: 130 } as const; // Type '{ readonly x: 50; readonly y: 130; }'
FOO_LOCATION.x = 10; // Error// ❌ Avoid
const BAR_LOCATION = [50, 130]; // Type number[]
BAR_LOCATION.push(10);
// ✅ Use
const BAR_LOCATION = [50, 130] as const; // Type 'readonly [50, 130]'
BAR_LOCATION.push(10); // Error// ❌ Avoid
const RATE_LIMIT = 25;
const RATE_LIMIT_MESSAGE = `Max number of requests/min is ${RATE_LIMIT}.`; // Type string
// ✅ Use
const RATE_LIMIT = 25;
const RATE_LIMIT_MESSAGE = `Max number of requests/min is ${RATE_LIMIT}.` as const; // Type 'Max number of requests/min is 25.''no-restricted-syntax': [ 'error', { selector: 'TSEnumDeclaration', message: 'Replace enum with a literal type or a const assertion.', }, ]// ❌ Avoid using enums as they increase the bundle size
enum UserRole {
GUEST = 'guest',
MODERATOR = 'moderator',
ADMINISTRATOR = 'administrator',
}
// Transpiled JavaScript
('use strict');
var UserRole;
(function (UserRole) {
UserRole['GUEST'] = 'guest';
UserRole['MODERATOR'] = 'moderator';
UserRole['ADMINISTRATOR'] = 'administrator';
})(UserRole || (UserRole = {}));
// ✅ Use literal types - Types are stripped during transpilation
type UserRole = 'guest' | 'moderator' | 'administrator';
const isGuest = (role: UserRole) => role === 'guest';// ❌ Avoid using enums
enum USER_ROLES {
guest = 'guest',
moderator = 'moderator',
administrator = 'administrator',
}
// ✅ Use const assertions arrays
const USER_ROLES = ['guest', 'moderator', 'administrator'] as const;
type UserRole = (typeof USER_ROLES)[number];
const seedDatabase = () => {
USER_ROLES.forEach((role) => {
db.roles.insert(role);
}
}
const insert = (role: UserRole) => {...
const UsersRoleList = () => {
return (
<div>
{USER_ROLES.map((role) => (
<Item key={role} role={role} />
))}
</div>
);
};
const Item = ({ role }: { role: UserRole }) => {...// ❌ Avoid using enums
enum COLORS {
primary = '#B33930',
secondary = '#113A5C',
brand = '#9C0E7D',
}
// ✅ Use const assertions objects
const COLORS = {
primary: '#B33930',
secondary: '#113A5C',
brand: '#9C0E7D',
} as const;
type Colors = typeof COLORS;
type ColorKey = keyof Colors; // Type "primary" | "secondary" | "brand"
type ColorValue = Colors[ColorKey]; // Type "#B33930" | "#113A5C" | "#9C0E7D"
const setColor = (color: ColorValue) => {...
setColor(COLORS.primary);
setColor('#B33930');// ❌ Avoid introducing multiple boolean flag variables
const isPending, isProcessing, isConfirmed, isExpired;
// ✅ Use type union variable
type UserStatus = 'pending' | 'processing' | 'confirmed' | 'expired';
const userStatus: UserStatus;strictNullChecksnullundefinednullundefined