在打字稿中as关键字的用法

在对象中,开发人员定义了如下代码常量:

medium: {
      fontFamily: 'Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif',
      fontWeight: '500' as '500',
    },

as关键字在做什么?

评论
  • 哎呀呀o
    哎呀呀o 回复

    referring to the answer in this question What does the "as" keyword do?

    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.

         var a = 'TEST STRING'
         var b = a as string; //Means the compiler will assume it's a string