profile
viewpoint

Ask questionsempty type is treated as any

Flow version: 0.125.1

Expected behavior

empty to behave like mixed (does not cast to anything only refinements are allowed)

Actual behavior

empty is basically alias to any

code

declare var nothing: empty
declare var something: 'cotne'

declare function giveMeNumber(number): void


giveMeNumber(1)// ok
giveMeNumber('cotne')// error as expected

giveMeNumber(nothing)// should error

if (something !== 'cotne') {
  giveMeNumber(something)// should error
}

Try

facebook/flow

Answer questions dsainati1

In this case empty is behaving as designed, that is to say, the exact opposite way as mixed. In Flow, mixed is the top type, the supertype of all other types. This means you cannot assume anything about its contents, but can create it from any value. For example, all of the following are legal: let x : mixed = 3, let x : mixed = {} and let x : mixed = () => {}, while all of the following are errors (assuming x has type mixed): x.foo, x() and x = x + 1. Put another way, you can write anything to a mixed value, but cannot read anything from it.

empty has the opposite behavior: it is the bottom type in Flow, the subtype of all other types. It is impossible to create, but if you obtain a value of type empty, you can do anything with it. So to contrast with the examples above, all of the following are errors: let x : empty = 3, let x : empty = {} and let x : empty = () => {}, while all of the following (assuming x has type empty) are legal: x.foo, x() and x = x + 1. Reading and writing empty are dual to mixed, you can read anything from an empty value, but cannot write anything to it.

In the example above, you can call giveMeNumber inside that refinement because Flow knows that the if statement will never succeed, so the code inside is dead; dead code is one of the only sources of empty in Flow. As such, the above code won't error because that function won't even get called.

As an aside, the reason any is an unsafe type is because it combines the permissibility of writing to mixed and reading from empty: you can put anything into an any value, and do anything you want with it.

useful!

Related questions

Common spread pattern leads to "exponentially large number of cases" errors hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Object.fromEntries type defs missing hot 1
Cannot set defaultProps with React.memo and React.forwardRef hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
0.92.0 release crashes on startup (Unix.Unix_error) hot 1
module.name_mapper does not find nested modules on windows hot 1
Cannot import useState because there is no useState export in react. hot 1
Can't use a default value from a destructuring assignment as a computed property since 0.111 hot 1
Support optional chaining - flow hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Allow explicitly including a folder under an ignored path hot 1
Flow caches and fails to purge errors hot 1
source:https://uonfu.com/
Github User Rank List