Quick Overview
`as` and `!` silence the compiler; they check nothing at runtime. Every assertion is a claim you are now responsible for, which is why a type guard is preferable wherever the check can actually be made.
Sometimes you know more about a value's exact type than TypeScript's static analyzer can infer (for example, when selecting a DOM element or parsing raw network payloads).
as)A Type Assertion tells the compiler "trust me, I know what type this value is":
// DOM element selection returns 'Element | null'
const inputElement = document.getElementById("username-input") as HTMLInputElement;
// Now TypeScript allows accessing HTMLInputElement specific properties!
console.log(inputElement.value);
as unknown as T)TypeScript prevents impossible assertions (e.g. asserting a number directly as a string). To bypass this check, perform a double assertion via unknown:
const rawValue = 42;
const forcedString = (rawValue as unknown) as string; // Force override
!)Appending an exclamation mark (!) after a variable asserts that a value is neither null nor undefined:
function processUser(user?: { name: string }) {
// Asserts user is definitely defined (bypasses strictNullChecks)
const name = user!.name;
}
inputElement is null at runtime, calling inputElement.value will crash!! bypasses type safety. Prefer optional chaining (?.) or explicit null checks over !.Use assertions sparingly when integrating third-party DOM or legacy JS libraries. Next, let's compare unknown versus any!
TypeScript erases entirely at build time, so a `string` annotation produces no runtime check whatsoever. A value arriving from an API is whatever the server actually sent, regardless of what the annotation claims.
`let x = "GET"` infers `string` because the binding can change; `const x = "GET"` infers the literal `"GET"`. That widening rule is why object properties need `as const` to stay narrow.
`string[]` is any number of strings; `[string, number]` is exactly two, in that order. `readonly` removes the mutating methods at compile time, so `push` becomes a type error rather than a runtime surprise.