DEV Community

Acid Coder
Acid Coder

Posted on • Updated on

Typescript WTF Moments 9: Evolving Empty Array Type

const a = false || []
//    ^?
const b = []
//    ^?
function c() {
//       ^?
  return []
}
const d = c()
//    ^?
Enter fullscreen mode Exit fullscreen mode

Image description

playground

More interactions:

let b = false || []
//  ^?
const a:number[] =  b
const c:string[] =  b

b = a // error as expected
b = c // expect as expected


let b1 = []
//  ^?
const a1:number[] =  b1 // did not expect error but error
const c1:string[] =  b1 // did not expect error but error

b1 = a1 
b1 = c1 

// with type annotation
let b2:any[] = []
//  ^?
const a2:number[] =  b2 
const c2:string[] =  b2 

b2 = a2 
b2 = c2 
Enter fullscreen mode Exit fullscreen mode

Image description
playground
The behavior of inferred any[] is very interesting, the declaration and assignment both error when we attempt to assign it

Image description

Turn out the inferred any[] type if not really an any[] type, it is an evolving type

Image description

playground

Oldest comments (0)