TypeScript - null versus undefined

后端 未结 3 515
挽巷
挽巷 2021-02-08 02:34

The TypeScript Coding Guidelines state

Use undefined. Do not use null

Having just read another article on ECMAScrip

3条回答
  •  隐瞒了意图╮
    2021-02-08 02:57

    I made some research few months ago and I came out with the fact that undefined must be prioritize using tslint rule 'no-null-keyword'.

    I tried to change my codebase and I had some issues. Why? Because I'm using an API that return null for empty fields.

    I struggled because of the tslint triple-equals rule.

    if (returnedData === undefined) // will be false because returnedData is null

    Which let you 2 options:

    1) Add some parameters to your triple-equals rules.

    "triple-equals": [true, "allow-null-check"] and do If (returnedData == null)

    allow-null-check allow "==" for null

    2) Use If (returnedData) instead but it checks if null/undefined/empty string or a zero

提交回复
热议问题