P
P
parse-g2022-02-19 18:01:51
typescript
parse-g, 2022-02-19 18:01:51

Why can't create type Object[] in typescript?

I have a simple code

class A<T extends Object[]> {
  public B:  T = [{ a: 1 }, { b: 2 }];
}


But an error comes up
Type "({ a: number; } | { b: number; })[]" cannot be assigned to type "T".
"({ a: number; } | { b: number; })[]" can be assigned to a constraint of type "T", but it is possible to instantiate "T" with a different constraint subtype "Object".ts(2322)


How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2022-02-19
@Leathi

In this case, Tit means any concrete type that is extends Object[].
for example new A<[{c: 3}]>()- it is absolutely valid, while it public Bcan no longer be what you put there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question