DEV Community

Acid Coder
Acid Coder

Posted on • Edited on

2 1

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay