Instruction file imported from kishimin-ai-create/diary (
.github/instructions/typescript.instructions.md). Copyright stays with the author.
TypeScript Rules
Type Assertions (as)
- Type assertions using
asare prohibited - If an assertion is absolutely necessary, you must add an explanatory comment on the line immediately before it
// NG
const el = document.getElementById("app") as HTMLDivElement;
// OK — getElementById can return null, but the element is guaranteed to exist in index.html
const el = document.getElementById("app") as HTMLDivElement;
any Type
- Use of the
anytype is prohibited - If
anyis absolutely necessary, you must add an explanatory comment on the line immediately before it
// NG
function parse(raw: any) { ... }
// OK — Using any temporarily because type definitions for this external library do not exist; will replace with unknown once type definitions are added
function parse(raw: any) { ... }
Alternatives
Before reaching for as or any, consider the following:
- Use type guard functions (
instanceof,typeof, custom type predicates) instead ofas - Use
unknowninstead ofany, and narrow the type with guards - If a type is truly unknown, handle it safely via the
unknown→ guard → concrete type flow