Frontend TypeScript guidelines¶
This guide describes how frontend code in app/frontend/src/ declares, imports, and derives TypeScript types. Formatting and syntax details belong to the lint and tsc configuration; the rules below cover decisions that need judgment.
Type public boundaries¶
Give an explicit type to everything another file or component consumes:
- component props, emits, models, and exposed members,
- exported functions, including their return type when it is not obvious from one expression,
- API service methods, store state, and exported constants with a structured shape.
Local variables and private helpers may rely on inference. Do not add annotations that only repeat what TypeScript already infers.
Avoid any. Use unknown for values whose shape is not known yet and narrow them before use (see Narrow unknown values).
Keep types with their owner¶
- Keep a type used by one file inside that file.
- Put a contract reused within a module into that module's
types/folder, for exampletypes/props.tsre-exported fromtypes/index.ts. - Put contracts shared across modules into the owning shared layer:
design_system/shared/types/for DS contracts such asDSSizingPropsinprops.ts, andshared/types/for application-wide types such asObjectValues. - Do not move a type to a shared folder only because it might be reused later.
Use type-only imports¶
Import types with import type (or an inline type modifier when a statement mixes values and types). Type-only imports are erased at build time, so they do not pull a module into the runtime bundle.
import type { Bookmark } from "@/src/bookmarks/types";
import { BOOKMARK_KIND, type BookmarkKind } from "@/src/bookmarks/constants/kind";
Use generated GraphQL types¶
Type GraphQL variables and results with the types generated next to each definition (*.generated.ts) and with schema types from shared/types/schemaTypes.generated.ts. Import them type-only and do not edit generated files. GraphQL API services shows a typed mutation.
When code needs a GraphQL schema enum value at runtime, use shared/types/schemaEnums.ts as described in Runtime schema enums.
Narrow unknown values¶
Treat caught errors, parsed JSON, localStorage values, and payloads from legacy code as unknown. Narrow them with typeof, instanceof, Array.isArray, in, or a type-guard function before reading properties. Avoid as casts that only silence the compiler.
const toError = (error: unknown): Error => {
return error instanceof Error ? error : new Error(window.EasyLocale.getLocale("error_something_went_wrong"));
};
Derive types from constant objects¶
Declare a fixed set of values once as an as const object and derive its union type with ObjectValues. Use the object for runtime values and the type for props and parameters.
import type { ObjectValues } from "@/src/shared/types/objectValues";
export const BOOKMARK_KIND = {
Page: "page",
Filter: "filter",
External: "external",
} as const;
export type BookmarkKind = ObjectValues<typeof BOOKMARK_KIND>;
Design System option constants such as DS_SIZE and the runtime schema enums in shared/types/schemaEnums.ts have the same shape.
Type constant maps¶
Type a lookup map with satisfies Record<Key, Value>. TypeScript then reports a missing or misspelled key when the key union changes, while as const keeps the literal value types.
const BOOKMARK_KIND_LABEL_KEY = {
[BOOKMARK_KIND.Page]: "bookmarks.kind_page",
[BOOKMARK_KIND.Filter]: "bookmarks.kind_filter",
[BOOKMARK_KIND.External]: "bookmarks.kind_external",
} as const satisfies Record<BookmarkKind, string>;
Use Partial<Record<...>> only when a missing key is a valid state.
Understand const and readonly¶
These tools protect different things:
constprevents rebinding the variable. It does not stop mutation of the object or array it holds.readonly,Readonly<T>,readonly T[], andas constare compile-time restrictions. They disappear in the emitted JavaScript and do not stop mutation at runtime.Readonly<T>andreadonlyproperties are shallow. Nested objects stay mutable unless their own types are readonly;as constapplies readonly to nested literals of the same expression.
Use readonly types for shared constants and for props or parameters a function must not mutate, such as bookmarks: readonly Bookmark[]. Copy a value before changing it instead of casting the readonly type away. Use Object.freeze only when runtime protection is actually required, and remember that it is shallow too.
Enums¶
Prefer as const objects with derived types for domain values and DS options, as shown above. TypeScript enum remains the established convention for test selectors (*DataCy and *Selector, see Frontend testing). Do not convert existing enums only for style.