as bypasses the compiler. Every as is a potential runtime crash the compiler can't catch.
The one exception: a cast immediately following exhaustive validation (as above) is acceptable because the cast is earned. But prefer a type guard or schema library (Zod, Valibot) over manual validation.
Refactoring as out of existing code: When encountering an as cast, determine why TypeScript can't infer the type. Usually one of:
최대한 유형이 안전한 코드를 작성하기 위한 TypeScript 유형 안전 지침입니다. TypeScript를 작성하거나 검토할 때 식별된 공용체, 유형 축소, 유형 가드, 완전성 검사, `as` 캐스트 방지, `any`보다 `unknown` 선호, 불가능한 상태를 표현할 수 없도록 만들기 등의 패턴을 적용합니다. TypeScript 코드를 작성할 때, 유형 안전성 문제에 대해 TypeScript를 검토할 때, 또는 사용자가 유형 안전성, 유형 축소, 차별적 공용체를 언급하거나 유형을 더 엄격하고 명시적으로 만들 것을 요청할 때마다 이 기술을 사용하세요. 출처: poteto/noodle.