Explain Type assertions in TypeScript Last Updated : 24 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In TypeScript, type assertions allow developers to override the compiler's inferred type, informing it of the specific type of a value. Type assertions are purely compile-time constructs and do not alter the runtime behavior of the code. They are particularly useful when interfacing with APIs or third-party libraries that return values of type any JavaScript let value: any = "This is a string"; let lengthOfString: number = (value as string).length; console.log(lengthOfString); A variable value is declared with the type any, which means it can hold any type of value.Using a type assertion (as string), the compiler is informed that the value should be treated as a string.The length property is then accessed safely because the compiler recognizes the value as a string.Output:16More Examples of Type Assertions in TypeScriptType Assertion with Function Return Value JavaScript function getValue(): any { return 'Hello, TypeScript!'; } let strLength: number = (getValue() as string).length; console.log(strLength); The getValue function returns a value of type any.By asserting the return value as a string, we can safely access the length property.Output:18 Type Assertion with DOM Element JavaScript let element = document.querySelector('input[type="text"]'); let inputElement = element as HTMLInputElement; console.log(inputElement.value); document.querySelector returns an Element type, which doesn't have a value property.By asserting element as HTMLInputElement, we inform TypeScript of the specific element type, allowing access to the value property.Output:[Value of the input field] Type Assertion with Union Types JavaScript type Pet = { name: string; walk: () => void; }; type Fish = { name: string; swim: () => void; }; let myPet: Pet | Fish = { name: 'Goldie', swim: () => console.log('Swimming') }; (myPet as Fish).swim(); myPet is of type Pet | Fish, meaning it could be either.By asserting myPet as Fish, we inform the compiler that myPet has a swim method, allowing its invocation.Output:SwimmingBest Practices for Using Type Assertions in TypeScriptUse Type Assertions Sparingly: Rely on TypeScript's type inference whenever possible. Overusing type assertions can mask potential errors and reduce code maintainability. Prefer 'as' Syntax: Use the as syntax for type assertions to avoid conflicts, especially in environments like JSX where the angle-bracket syntax can cause issues. Avoid Asserting to 'any': Asserting a value to any negates the benefits of TypeScript's static typing. Instead, define precise types to maintain type safety. Comment More info S sarahjane3102 Follow Improve Article Tags : TypeScript Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like