Constants
Example: message constants and UI offsets
import { EasyAIMessage, MessageOriginType, StreamMessageType } from "../types/message";
export const EMPTY_STREAM_AI_MESSAGE: Readonly<EasyAIMessage> = {
origin: MessageOriginType.Stream,
message: "",
type: StreamMessageType.Message,
};
export const ACTIVATOR_POSITION_OFFSET_TOP_PX = 25;
export const ACTIVATOR_POSITION_OFFSET_LEFT_PX = 5;
Usage
import { EMPTY_STREAM_AI_MESSAGE } from "@/src/easy-ai/assistant/constants";
function createInitialMessage() {
return { ...EMPTY_STREAM_AI_MESSAGE }; // Safe to copy, can't be mutated
}
Best practices
- Group constants by purpose into logically named files (e.g.,
constants/ui.ts, constants/messages.ts).
- If a module has only a few constants (1-4), it is allowed to use an
index.ts file in the constants folder for simplicity.
- Use explicit type annotations for structured or domain-specific constants.
- Write descriptive names that clarify intent and usage.
- Use
export const to guarantee immutability.
- Use
UPPER_SNAKE_CASE for all constant names.
- Include unit suffixes in names when applicable (e.g.,
_PX, _MS, _CHARS) to clarify what the value represents.
- Use
Readonly<Type> for object constants to prevent accidental mutation.