What does "as" keyword do in JS?

  if (process.env.NODE_ENV !== 'production') {
    (WithUser as any).displayName = wrapDisplayName(Component, 'withUser');

I'm not sure whether as is a keyword or not. But what does "as" keyword do in js?

1 answer

  • answered 2019-04-21 09:37 Adrian Brand

    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.