prefer-type-annotations

Enforce TypeScript type annotations over type assertions in code.

Updated Jul 17, 2017
One-click install
npx skills add https://github.com/luyi985/lyi-bash --skill prefer-type-annotations
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: prefer-type-annotations
Source: https://github.com/luyi985/lyi-bash/tree/main/ai/skills/prefer-type-annotations
Command: npx skills add https://github.com/luyi985/lyi-bash --skill prefer-type-annotations

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps developers avoid unsafe type assertions by enforcing explicit type annotations in TypeScript, ensuring values conform to defined interfaces at compile time.

Core Features & Use Cases

  • Type annotations validate values, function returns, and object literals against defined types.
  • Discourages use of as type assertions that bypass checks; promotes safer, more maintainable code.
  • When assertions are truly needed, provides guidelines and examples to document rationale and ensure safety.

Quick Start

Ask your codebase to replace type assertions with appropriate type annotations to enforce safety.

Frequently Asked Questions about prefer-type-annotations

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why should I use type annotations instead of type assertions in TypeScript?

Type annotations validate values against defined interfaces at compile time, whereas type assertions bypass static type checking. Using annotations over assertions improves runtime safety and code maintainability by ensuring values conform to defined types.

How do I replace type assertions with type annotations for object literals?

To replace type assertions for object literals, define explicit interfaces and apply them as type annotations during variable assignment. This enforces compile-time validation instead of using the 'as' keyword to bypass structural checks.

When is it acceptable to use type assertions in TypeScript?

Type assertions are acceptable only when truly needed, provided you include an inline justification documenting the rationale. This ensures safety by explicitly acknowledging that the standard type annotation validation is intentionally bypassed.

How do type annotations improve function return safety in TypeScript?

Type annotations improve function return safety by explicitly defining the expected return type. This enforces compile-time static type checking, ensuring the function output strictly conforms to the defined interface rather than relying on inferred or asserted types.

Does replacing type assertions with type annotations slow down development?

Replacing type assertions with type annotations requires explicit type definitions for variable assignments and function returns. While this adds upfront definition effort, it prevents runtime errors by enforcing static type checking during development.