TypeScript: Implicit number to enum cast

后端 未结 2 1396
野性不改
野性不改 2020-12-11 16:23

Why does the following compile in TypeScript?

enum xEnum {
  X1,X2
}

function test(x: xEnum) {
}

test(6);

Shouldn\'t it throw an error? I

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 16:53

    No, it shouldn't. There is no type casting here, the base type behind them all is the same, integer.

    typescript enum type checking works fine

    Your complaint is about range value which, in this case, has nothing to do with type checking.

    enum is a flexible set of constants

    enum xEnum {X1=6, X2} // ruins it for test(0)
    

提交回复
热议问题