Trying to understand the purpose of keyword "number"
Hello I've been following the web tutorial that theo put out and Im at the part where he brings in a type from trpc and he defines that by putting in [number] at the end. I'm just confused on the significance of that keyword is that JavaScript thing. I have never seen it before. Im just looking for an explanation. Thanks
4 Replies
It's not js thing it's ts
It's the indexed type access
If you want to check for more
https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html
Documentation - Indexed Access Types
Using Type['a'] syntax to access a subset of a type.
This is related to arrays. You see, when you call an array and want to get one item in it, you give out an index.
Ex:
The [number] in typeProps you are seeing is like accessing the type of the array type
Im bad at explaining but I hope it made sense
The number part in the end is for getting the post type if im correct
From that line I'm guessing you get all posts as array and saying [number] you get the type of the posts inside this array
The SecondTypeFromArray will be an
object
.
The TypeOfAnyArray will be string | object
Also, yes. It's a typescript thing not javascript. All related to types