prefer-unknown-over-any

Replace any with unknown for untyped values and validate before use.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Apply unknown instead of any when values have an unknown type, such as data from JSON, external sources, or untrusted inputs, reducing runtime errors and improving type safety.

Core Features & Use Cases

  • Use unknown to force narrowing before use, preventing unsafe operations on values with unknown type.
  • Encourage safer APIs by returning unknown until the value is validated.
  • Apply in real-world scenarios like JSON parsing, external API responses, and user input handling to ensure safe usage.

Quick Start

Replace any with unknown when you don't know a value's type, then validate before use.

Frequently Asked Questions about prefer-unknown-over-any

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

FAQPage Schema
Why should I use unknown instead of any for TypeScript JSON parsing?

You should use unknown instead of any for TypeScript JSON parsing because unknown forces you to validate and narrow types before usage, preventing unsafe operations and runtime errors on untyped data.

How do I handle external API responses in TypeScript without runtime errors?

To handle external API responses safely, type the incoming data as unknown and apply type guards and validations to narrow the type before accessing properties, preventing unexpected runtime errors.

What is the best way to process untyped user input in TypeScript?

The best way to process untyped user input is to assign it the unknown type, which enforces type checking and requires explicit validation before the value can be safely used in your application.

Does using unknown over any improve type safety for external data ingestion?

Yes, using unknown over any improves type safety for external data ingestion by forcing developers to perform type narrowing and validation, ensuring that untrusted data is structurally verified before execution.

How do I replace any with unknown in TypeScript for safer APIs?

To replace any with unknown, update your function signatures to return unknown for untrusted values, then implement type guards to narrow the data structure before it is accessed by the application.

When should I not use the unknown type in TypeScript?

You should avoid using unknown when you are certain of a value's type and structure, as applying it unnecessarily requires redundant type guards and narrowing that can complicate code without adding safety.