Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the difference between these type assertion or casting methods in TypeScript

I'm fairly new with TypeScript, could you explain what are the difference between these methods of type assertion:

// 1. Using :
let myStr: string;

// 2. Using as
let strLength = (myStr as string).length;

// 3. Using <> on left side
let strLength = <string>myStr.length;

// 4. Using <> on right side
let myObs: Observable<number>

and when to use one over the others? Thanks

like image 480
xcode Avatar asked Apr 13 '18 13:04

xcode


People also ask

What are type assertions in TypeScript?

In Typescript, Type assertion is a technique that informs the compiler about the type of a variable. Type assertion is similar to typecasting but it doesn't reconstruct code. You can use type assertion to specify a value's type and tell the compiler not to deduce it.

What is type casting in TypeScript?

Typecasting, commonly known as type conversion, is a technique for transforming one type into another in TypeScript. Typecasting exists, even though they don't work in the same way in strong programming languages. It exists in Object-oriented programming, but it does not exist in JavaScript.

Why you should avoid type assertions in TypeScript?

It weakens Type Safety Because of this, type assertions are considered an anti-pattern or code smell unless the user is absolutely sure what they are doing. For example, in some advanced/fancy use case of type hacking, tooling or when the typings of a third-party library is not accurate.

What is type inference in TypeScript?

TypeScript infers types of variables when there is no explicit information available in the form of type annotations. Types are inferred by TypeScript compiler when: Variables are initialized. Default values are set for parameters. Function return types are determined.


2 Answers

// 1. Using :
let myStr: string;  // declaring a variable with its type as string

// 2. Using as
let strLength = (myStr as string).length; // casting a variable's type to string type using `as` keyword,but here type of strLength is determine by type assertion as there is no explicit type defined.

// 3. Using <> on left side
let strLength = <string>myStr.length;  // same as 2 above but using <>

// 4. Using <> on right side
let myObs: Observable<number>  // Observable is a generic type you can specify its type(T) in  Observable<T>, here it is `number` type.

However there is an ambiguity in the language grammar when using <> style assertions in JSX, hence recommended to use as for consistency.

Typescript casting :

https://acdcjunior.github.io/typescript-cast-object-to-other-type-or-instanceof.html

https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html

Typescript generic type : https://www.typescriptlang.org/docs/handbook/2/generics.html

like image 181
Niladri Avatar answered Sep 29 '22 01:09

Niladri


The first one, you're setting the type of the variable as a string, so if you try to set the variable with something else than a string it will throw an error

let myStr: string
myStr = 1 // not working
myStr = 'Hello' // Working

the second one is a cast, the variable has a type but you want to use it as another type. Example, a function return an object but you know it's a string and want to use it as a string:

let myStr = helloWorld() //return an object but you know it's a string
strLength = (myStr as string).length //get length of the string

The third one is the same than above

and last one, you're setting the type of the object contained in your Observable so in your example, myObs is waiting for string and if you had something else it won't work, it's generic you can put whatever you want

Observable<T>

Some links http://www.typescriptlang.org/docs/handbook/advanced-types.html

like image 39
Lucas Tambarin Avatar answered Sep 29 '22 03:09

Lucas Tambarin