if (process.env.NODE_ENV !== 'production') { (WithUser as any).displayName = wrapDisplayName(Component, 'withUser'); }
I'm not even sure if as
is a keyword, but anyway, what does it do in JavaScript?
as any tells the compiler to consider the typed object as a plain untyped JavaScript object. The as keyword is a Type Assertion in TypeScript which tells the compiler to consider the object as another type than the type the compiler infers the object to be.
Identifiers in TypeScript Identifiers are names given to elements in a program like variables, functions etc. The rules for identifiers are − Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
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.
The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.
That is not vanilla JavaScript, it is TypeScript. As any means consider the typed object as a plain untyped JavaScrpt object.
The as
keyword is a Type Assertion in TypeScript which tells the compiler to consider the object as another type than the type the compiler infers the object to be.
It's TypeScript, not vanilla JS, but for the as
itself: It's called Type Assertion, you are just telling the compiler to treat something as a type:
var a = 'TEST STRING' var b = a as string; //Means the compiler will assume it's a string
It's equivalent to this:
var a = 'TEST STRING' var b = <string> a;
However it might be confusing when working with JSX (JS with html tags) , so in those cases the as
syntax is preferred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With