TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
is keyword in TypeScript is a type predicate operator used within a function’s return type annotation to create a user-defined type guard. It instructs the TypeScript compiler’s control flow analysis to narrow the type of a specific parameter to a declared type within the caller’s scope when the function evaluates to true.
Syntax
The syntax requires the parameter name, theis keyword, and the target type. The parameter name referenced in the predicate must exactly match a parameter name in the function signature.
Mechanics and Control Flow Analysis
Theis operator bridges runtime boolean evaluation and the compile-time type system. When a function annotated with parameterName is Type is invoked in a conditional statement (such as an if block), the compiler applies the following control flow logic:
- On
true: The compiler narrows the type of the passed argument from its base type to the target type within theifbranch. - On
false: If the parameter’s base type is a union and the target type is a constituent of that union, the compiler narrows the type to the complement of the target type (e.g., if the base type isstring | numberand the predicate checks forstring, thefalsebranch narrows the type tonumber). However, if the base type isunknownorany, thefalsebranch does not narrow to a complement type; the parameter remainsunknownorany.
boolean if the function body narrows the parameter. Explicitly declaring the is operator enforces the type narrowing contract in the function signature. This explicit declaration is necessary for interface definitions, complex logic that exceeds the compiler’s inference capabilities, or ensuring strict API stability.
Class Context (this is Type)
In object-oriented patterns, the is operator can be applied to the this context of a class or interface method. This allows instance methods to narrow the type of the class instance itself within the caller’s scope.
Type Safety and Assertions
Explicit type predicates (parameterName is Type) are intentionally unchecked by the TypeScript compiler. They act as unsafe type assertions, meaning the compiler trusts the developer’s explicit annotation regardless of the function’s internal logic. If the function body’s implementation does not strictly guarantee the narrowed type, the compiler will not emit a type error. The responsibility lies entirely with the developer to ensure that the runtime boolean logic accurately reflects the compile-time type assertion to prevent type unsafety.
Master TypeScript with Deep Grasping Methodology!Learn More





