打字稿中as关键字的使用

问题描述 投票:0回答:1

开发人员在一个对象中定义了这样的代码常量:

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

as关键字在做什么?

typescript tsc react-native-paper
1个回答
0
投票

指此问题的答案What does the "as" keyword do?

as关键字是TypeScript中的类型断言,它告诉编译器将对象视为不同于编译器推断为对象的类型的另一种类型。

     var a = 'TEST STRING'
     var b = a as string; //Means the compiler will assume it's a string
© www.soinside.com 2019 - 2024. All rights reserved.