typescript syntax: { new (): T } | { new (): T }[]

Viewed 85

I was looking through the Vue api docs and noticed some Typescript syntax that doesn't make sense to me

type PropType<T> = { new (): T } | { new (): T }[]

Is { new (): T } an Object with a constructor method that returns the generic type T, is that what this means?

1 Answers

Yes, it is an union between two construct signatures:

type PropType<T> = { new (): T } | { new (): T }[]

class A {}

const b = (a: PropType<A>) =>
  a instanceof Array ? [] : new a();

Because it is a union this needs type narrowing so the compiler can pluck the right type.

Related