Skip to content
Back to skills

Any Type

ASecurity

Avoid using any type in TypeScript. Use unknown, generics, or proper type definitions instead. Use when writing or reviewing TypeScript type annotations.

  • 3 stars
  • 0 votes
  • 0 copies
  • 6 views
  • Added May 27, 2026
data-aitypescriptgoapi

Works with

  • api

Security analysis

A100/100

Scanned May 27, 2026

npx -y skills add ncaq/konoka --skill any-type --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Any Type?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Any Type
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ncaq-any-type/badge)](https://www.skillsdirectory.com/skills/ncaq-any-type)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: any-type
description: Avoid using any type in TypeScript. Use unknown, generics, or proper type definitions instead. Use when writing or reviewing TypeScript type annotations.
user-invocable: false
---

# `any`型を避ける

`any`型はTypeScriptの型チェックを完全に無効化するため、使用しないでください。
`any`を使った時点で、その値に対するあらゆる操作がコンパイル時にチェックされなくなり、
実行時エラーの原因になります。

ESLintでも`@typescript-eslint/no-explicit-any`ルールとしてよく禁止されています。

## 代替手段

### `unknown`を使う

型が不明な値を受け取る場合は`unknown`を使ってください。
`unknown`は`any`と同様にあらゆる値を代入できますが、
使用する前にtype guardで絞り込む必要があるため型安全です。

```typescript
// Bad
function parse(input: any): string {
  return input.name;
}

// Good
function parse(input: unknown): string {
  if (typeof input === "object" && input != null && "name" in input) {
    return String(input.name);
  }
  throw new Error("Invalid input");
}
```

### ジェネリクスを使う

型を呼び出し側に委ねたい場合はジェネリクスを使ってください。

```typescript
// Bad
function first(arr: any[]): any {
  return arr[0];
}

// Good
function first<T>(arr: T[]): T | undefined {
  return arr[0];
}
```

### バリデーションライブラリを使う

外部からのデータ(APIレスポンス、フォーム入力など)はバリデーションライブラリでパースしてください。
`unknown`からの手動type guardよりも安全かつ宣言的です。

```typescript
// Good: EffectのSchemaモジュールの例
import { Schema } from "effect";

const User = Schema.Struct({
  id: Schema.String,
  name: Schema.String,
});

const response = await fetch("/api/users").then((r) => r.json());
const users = Schema.decodeUnknownSync(Schema.Array(User))(response);
// users: { readonly id: string; readonly name: string }[]
```

### `Record`を使う

キーと値の型が分かっているオブジェクトには`Record`を使ってください。

```typescript
// Bad
const cache: any = {};

// Good
const cache: Record<string, User> = {};
```

## `any`が許容される場面

- サードパーティライブラリの型定義が壊れていて、型を正しく表現できない場合
- 既存コードの段階的な型付けにおいて、一時的に`any`を使う場合

いずれの場合も`// eslint-disable-next-line @typescript-eslint/no-explicit-any`のような抑制コメントで意図を明示してください。

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.