Operator '==' cannot be applied to types x and y in Typescript 2

后端 未结 4 1015
梦谈多话
梦谈多话 2020-12-10 00:59

TypeScript Version: 2.0.2.0

Code I know the code is a bit stupid, but I actually have these kind of tests in my code (m

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 01:25

    why aren't 5 and 2 considered as type 'number'

    The have the literal type 5 and 2. e.g.

    var x: 5; 
    // can only ever be assigned to 5 
    x = 5; // okay 
    x = 2; // Error 
    

    I don't see an actual use case for wanting it not to be an error. This is just the compiler trying to help you. Feel free to create an issue if you see sufficient motivation

提交回复
热议问题