Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

How to make these two type definitions exactly the same?

I'm wondering if there is another way to write a maybe type.

type Error = ?{name: string, message:string}
type Error = {name: string, message:string} | null

The above isn't the same since the undefined case isn't allowed in the second definition, but it is in the first. (source)

However, the below doesn't work.

type Error = {name: string, message:string} | null | undefined

Is there another way to represent a maybe type without using the ? prefix?

Upvotes: 0

Views: 46

Answers (2)

Aleksey L.
Aleksey L.

Reputation: 37938

In Flow the type of undefined is void, so:

type Error = {name: string, message:string} | null | void

Docs

Upvotes: 2

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

This works!

type Error2 = {name: string, message:string} | null | typeof undefined

Upvotes: 0

Related Questions