3 ms·
TypeScript widens the type of x to allow `number | string`, there are no type errors below: const x = [] x.push(1) type t = typeof x // ^? ty
by jitl 10mo ago
TypeScript widens the type of x to allow `number | string`, there are no type errors below:
const x = []
x.push(1)
type t = typeof x
// ^? type t = number[]
x[0] = "new"
type t2 = typeof x
// ^? type t2 = (number | string)[]
const y = x[0] + "oops"
// ^? const y: string
https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgZ0QAoBKRAbwChFFRJYEVVt0wATAsym2xCBASiJUiALyIA2gF1etVADpsIAgAtiARlJzEUAJ7Zku8boPI4wEToD01vgD0A-DsXK1xAER6P2vqcOIeib6hhZWfrYOzrQAvlS89NDwSABOyHjoEITc1H4CYEIiJjIuSirqWjohRsIS1WGoNna0Ti6SAAzSJh5gyADuHlVmugBMwWYNTVE6+YVBEqgdXQDUiB5wcNgEgxHNLdGIcbxpUCApSBTMrBxEcTFAA https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABA...