#I've got an array of types, but can't figure out how to narrow it

1 messages · Page 1 of 1 (latest)

rose heath
#

This is jsdoc, but ts is behaving the same way. I've got an array of types that extend from the same base class. I can't figure out how to filter down and have the type narrowed. I want to avoid specifically casting it if I can.

Here, arr2 and arr3 are still typed as Foo[] -- what options do I have so that filtering changes the type of the array?

crisp sapphireBOT
#
claudekennilol#0

Preview:```ts
console.clear()

class Foo {
static printFoo() {
console.log("this is Foo")
}
}

class Bar extends Foo {
static printBar() {
console.log("this is Bar")
}
}

/** @type {(typeof Foo)[]} */
const arr = [Foo, Bar, Foo, Bar, F
...```