R
R
RDMStreet2020-09-03 21:04:45
typescript
RDMStreet, 2020-09-03 21:04:45

Why is TypeScript not inferring the type correctly?

There is a request through the prisma library that returns a value with the User[] type. Why do I get a variable of type User and not User when I get element with index 0 from this array | undefined? The array can be empty.

5f5130ed7ab23056131042.png
5f51305170cba114701654.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-09-03
@RDMStreet

Because, in fact, under the hood is this type:

type Array<T> = {
  length: number;
  [i: number]: T;
} & ArrayMethods<T>;
where ArrayMethods is the description of the methods from the prototype

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question