Skip to content
Back to skills

Non Null Assertion

ASecurity

Do not use non-null assertion operator (!) in TypeScript. Use optional chaining, type guards, or nullish checks instead. Use when writing or reviewing TypeScript code.

  • 3 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added May 27, 2026
data-aitypescriptnode

Security analysis

A100/100

Scanned November 28, 2026

npx -y skills add ncaq/konoka --skill non-null-assertion --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Non Null Assertion?

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

Security grade badge for Non Null Assertion
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ncaq-non-null-assertion/badge)](https://www.skillsdirectory.com/skills/ncaq-non-null-assertion)

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: non-null-assertion
description: Do not use non-null assertion operator (!) in TypeScript. Use optional chaining, type guards, or nullish checks instead. Use when writing or reviewing TypeScript code.
user-invocable: false
---

# Non-null assertion(`!`)を使わない

Non-null assertion演算子(`!`)は使わないでください。
付けた時点でコンパイラによるnullチェックが無効になり、実行時エラーの原因になるためです。
ESLintでも`@typescript-eslint/no-non-null-assertion`ルールとしてよく禁止されています。

代わりに以下のように書きます。

- 値がnullableかもしれない場合は`?.`でアクセスします。
  `users[0]!.name`は`users[0]?.name`か`users.at(0)?.name`と書きます。
- `Map`からの取得のように型が`T | undefined`になる場面では、nullチェックで絞り込みます。
- 配列からnullableな要素を除くには`filter`を使います。
  TypeScript 5.5以降では、明示的な型述語を書かなくても`filter`で型が絞り込まれます。

```typescript
// 避ける
const config = configMap.get(key)!;
const names = users.map((u) => u.name!);

// 良い
const config = configMap.get(key);
if (config == null) {
  throw new Error(`Config not found for key: ${key}`);
}
const names = users.map((u) => u.name).filter((name) => name != null);
// names: string[]
```

## テストコード

jestやvitestの`expect(value).toBeDefined()`を書いても、TypeScriptの型はnullableのままです。
`expect`の後に`!`を付けるのではなく、Node.jsかvitestの`assert`で絞り込んでください。

```typescript
import assert from "node:assert";

const item = items.find((i) => i.id === targetId);
assert(item != null);
expect(item.name).toBe("foo");
```

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.

View all in data-ai →

Comments

Loading comments…

Newsletter

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