| name | typescript-strict |
| description | Enforce TypeScript strict mode and type safety. Use when setting up projects, reviewing code, or when type errors are ignored. Covers strict flags, no-any rules, and type inference best practices. |
| allowed-tools | Read, Glob, Grep, Edit, Write, Bash |
| license | MIT |
| metadata | {"author":"antigravity-team","version":"1.0"} |
TypeScript Strict Mode
TypeScript 엄격 모드와 타입 안전성을 강제하는 스킬입니다.
2025 Context
TypeScript 5.x에서 strict 모드가 새 프로젝트의 기본값으로 권장됨
"any 사용은 TypeScript를 쓰는 의미를 없앤다"
Core Rules
| 규칙 | 상태 | 설명 |
|---|
strict: true | 🔴 필수 | 모든 엄격 검사 활성화 |
any 금지 | 🔴 필수 | unknown 또는 제네릭 사용 |
// @ts-ignore 금지 | 🔴 필수 | 타입 에러 해결 필수 |
as 캐스팅 최소화 | 🟡 권장 | 타입 가드 우선 |
tsconfig.json 권장 설정
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"exactOptionalPropertyTypes": true,
"noPropertyAccessFromIndexSignature":
any 금지
문제: any 사용
function processData(data: any) {
return data.value;
}
const result: any = fetchData();
result.nonExistent();
해결: unknown 또는 타입 명시
function processData(data: unknown) {
if (isValidData(data)) {
return data.value;
}
throw new Error('Invalid data');
}
function isValidData(data: unknown): data is { value: string } {
return typeof data === 'object'
&& data !== null
&& 'value' in data;
}
function processData<T extends { value: string }>(data: T) {
return data.value;
}
any → unknown 마이그레이션
function parse(json: string): any {
return JSON.parse(json);
}
function parse(json: string): unknown {
return JSON.parse(json);
}
const result = parse('{"name": "test"}');
if (isUser(result)) {
console.log(result.name);
}
타입 단언(as) 최소화
문제: 과도한 타입 단언
const user = response.data as User;
user.name.toUpperCase();
const value = data as unknown as TargetType;
해결: 타입 가드 사용
function isUser(data: unknown): data is User {
return (
typeof data === 'object' &&
data !== null &&
'name' in data &&
typeof (data as { name: unknown }).name === 'string'
);
}
if (isUser(response.data)) {
response.data.name.toUpperCase();
}
import { z } from 'zod';
const UserSchema = z.object({
name: z.string(),
email: z.string().email(),
});
const user = UserSchema.parse(response.data);
Null 안전성
strictNullChecks 활용
function getLength(str: string | null) {
return str.length;
}
function getLength(str: string | null) {
if (str === null) return 0;
return str.length;
}
function getLength(str: string | null) {
return str?.length ?? 0;
}
배열 인덱스 접근
const arr = [1, 2, 3];
const first = arr[0];
console.log(first.toFixed(2));
if (first !== undefined) {
console.log(first.toFixed(2));
}
console.log(arr[0]?.toFixed(2) ?? 'N/A');
함수 타입
반환 타입 명시 (권장)
function fetchUser(id: string) {
return api.get(`/users/${id}`);
}
async function fetchUser(id: string): Promise<User> {
return api.get(`/users/${id}`);
}
함수 오버로드
function process(input: string): string;
function process(input: number): number;
function process(input: string | number): string | number {
if (typeof input === 'string') {
return input.toUpperCase();
}
return input * 2;
}
const str = process('hello');
const num = process(42);
제네릭 활용
function first(arr: any[]): any {
return arr[0];
}
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
ESLint 규칙
{
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"rules": {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-return": "error",
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/prefer-nullish-coalescing": "warn"
}
}
금지 패턴
data as any
(data as unknown) as TargetType
data!
data as Type
Workflow
1. 새 프로젝트 설정
npx tsc --init
grep -n "strict" tsconfig.json
2. 기존 프로젝트 마이그레이션
npx tsc --noEmit
3. 코드 리뷰 체크
타입 안전성 체크:
- [ ] any 사용하지 않음
- [ ] @ts-ignore 없음
- [ ] 타입 단언 최소화
- [ ] null 체크 적절함
Checklist
References