HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptCriticalCanonical

What does the "as" keyword do?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
doeswhatkeywordthe

Problem

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?

Solution

That is not vanilla JavaScript; it is TypeScript. 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.

Context

Stack Overflow Q#55781559, score: 340

Revisions (0)

No revisions yet.